Can I disable home button On Android - java

I can not disable home button and Which is next to it in android I tried this code but does not work
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
Do you have any idea for solve this problem?
Or Can I hide navigation bar permanently forever؟

Can I hide navigation bar permanently forever?
No, system would not allow you to do so. But you can prevent the app from exiting.
Do the following in your onPause() method.
#Override
protected void onPause() {
super.onPause();
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
You need the following permissions
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.REORDER_TASKS" />

I found it.
I took a part from here
how to disable home button in android?
and added one row Which originally existed
LAUNCHER row as follow:
<activity android:name=".MainActivity"
android:clearTaskOnLaunch="true"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

Related

[Android]: App behavior when receiving notifications on the splash screen and main screen

I'm using flutter and I'm a bad understand native android development very well.
I'm using 2 Activities in my project:
SplashActivity - visible 1 time at the time of launching the app.
MainActivity - the main part of the application.
The problem is that when receiving push-notification from Firebase and when clicking on the notification, the program is completely restarted starting from SlashActivity. I added isTaskRoot to my SplashActivity, but it turned out not to be true, because my notification, when clicked, stopped calling a callback(), only expands the app or opens it starting with SplashActivity.
How it should work:
If MainActivity already exists (either in the background or foreground), open that same instance.
If MainActivity was destroyed (the user previously pressed Back), then open the SplashActivity. The splash activity will redirect you itself to MainActivity.
Notification should be targeted at the MainActivity and changed flow (callback), in depend on the previous points.
#SuppressLint("CustomSplashScreen")
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// bad decision
if (!isTaskRoot()) {
finish();
return;
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.launch_screen);
new Handler().postDelayed(() -> {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
}, 2000);
}
}
import io.flutter.embedding.android.FlutterFragmentActivity
class MainActivity: FlutterFragmentActivity() {
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="example"
android:name="${applicationName}"
android:icon="#mipmap/ic_launcher">
<activity
android:name=".SplashActivity"
android:parentActivityName=".MainActivity"
android:exported="true"
android:allowBackup="true"
android:launchMode="singleTask"
android:theme="#style/AppTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background"
/>
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="#style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:theme="#style/LaunchTheme">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="#style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorNotification" />
</application>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
</manifest>
I/flutter (16767): onMessage event: Notification text - Test a body message
E/libc (16767): Access denied finding property "ro.vendor.df.effect.conflict"
E/libc (16767): Access denied finding property "ro.vendor.knock.type"
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/example.app(16767): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
W/FirebaseMessaging(16767): Unable to log event: analytics library is missing
W/FirebaseMessaging(16767): Unable to log event: analytics library is missing
D/DecorView[](16767): getWindowModeFromSystem windowmode is 1
D/DecorView(16767): createDecorCaptionView windowingMode:1 mWindowMode 1 isFullscreen: true

My landscape android application turns to the portrait and crash [duplicate]

I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity> element/s in
the manifest and you're done.
You can follow the logic below to prevent auto rotate screen while your AsyncTask is running:
Store your current screen orientation inside your activity using getRequestedOrientation().
Disable auto screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
Run/execute your AsyncTask.
At the end of your AsyncTask restore your previous orientation status using setRequestedOrientation(oldOrientation).
Please note that there are several ways to access Activity (which runs on UI thread) properties inside an AsyncTask. You can implement your AsyncTask as an inner class or you can use message Handler that poke your Activiy class.
The easiest way I found to do this was to put
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
setContentView(R.layout.activity_main);
so...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
Rather than going into the AndroidManifest, you could just do this:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
#Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
Activity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="QRCodeActivity" android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
User "portrait" in your AndroidManifest.xml file might seem like a good solution. But it forces certain devices (that work best in landscape) to go into portrait, not getting the proper orientation. On the latest Android version, you will get wearing an error. So my suggestion it's better to use "nosensor".
<activity
...
...
android:screenOrientation="nosensor">
Add the following to your AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
android:configChanges="orientation"
So, the full activity node would be:
<activity android:name="Activity1"
android:icon="#drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
Prevent Screen Rotation just add this following line in your Manifests.
<activity
android:name=".YourActivity"
android:screenOrientation="portrait" />
This works for me.
android:screenOrientation="portrait"
on application tag <application
and
<activity
...
...
android:screenOrientation="locked">
If you are using Android Developer Tools (ADT) and Eclipse
you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. Finally, select your preferred orientation.
You can select one of the many options.
You have to add the following code in the manifest.xml file.
The activity for which it should not rotate, in that activity add this element
android:screenOrientation="portrait"
Then it will not rotate.
You can try This way
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Use AsyncTaskLoader to keep your data safe even if the activity changes, instead of using AsyncTask that is a better way to build apps than preventing screen rotation.

how can I launch a first time installation project?

I have an android project for first time installation.
It is related with my firm agreement pages. It comes after google agreement pages.
I tried some technics for doing it. For example,I set it as a system application. However, it is cleaned when backup and reset operation.
Does anybody know how to run it?
on AndroidManifest.xml
<application
android:theme="#android:style/Theme.NoTitleBar"
android:allowBackup="true"
android:icon="#drawable/launcher"
android:label="#string/appname" >
<activity
android:name="xxxagreement.MainActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter android:priority="3">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
on MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
Intent i = new Intent();
i.setAction(AGREEGATE_STARTED);
sendBroadcast(i);
TextView t = (TextView)findViewById(R.id.textView1);
t.setSelected(true);
...
super.onResume();
}
Event Log prints:
Session 'app': Error Launching activity
Your application doesn't have any launcher activity. Replace these lines
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
with
<category android:name="android.intent.category.LAUNCHER" />

Linking Activities Using Intents (Android Beginner)

I'm beginning to learn Android and have been reading the "Beginning Android 4 Application Development." (As well as downloading the relevant source code)..
However; I have been trying to create a very simple slideshow with a button labelled "Gallery" which will take me to a new Activity that will show a grid like layout for my photos. However, my application does not do this. When the button is pressed it either crashes the app or refuses to do anything at all.
Manifest
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="Viwer"
android:name=".Viwer" >
<intent-filter >
<action android:name="com.example.viwer.Gallery" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Any help would be greatly appreciated, I assume it's something simple but after looking at it for three hours I can't see what it is.
You need to add the Gallery activity to the Android Manifest
<activity
android:name=".Gallery"
android:label="Gallery"></activity>
In your manifest you register viewer activity in an intent-filter yet is not declare as a broadcast receiver.
Second event if it was a broadcast receiver it will never launch as you do not send any broadcast by calling startActivity
i think that what your trying to achieve is this:
Please try this:
ebutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Viwer.calss )
startActivity(intent);
}
});

Disable Home Button in Homescreen app?

I am working on a research project for my university. The app will never be put on the market and only used for research.
I have made a homescreen app by using the Google Homescreen sample code.
In there, I have made an activity that is a lockscreen.
While in there, the user should not be able to get out of the lock by pressing Home, Back, etc.
The Back-Button seems to be disabled, but the Home-Button is not.
I have tried several solutions from the internet and stackoverflow, which are all not working.
Here is the important code:
(Notice: Logcat shows "Button pressed: 4" for the Back-Button but nothing for the home button!)
In my Lock-Screen Activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.v(TAG, "BUTTON PRESSED: " + new Integer(keyCode).toString());
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
return true;
} else if ((keyCode == KeyEvent.KEYCODE_CALL)) {
return true;
}
else if ((keyCode == KeyEvent.KEYCODE_HOME)){
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
It seems like the onAttachedToWindow() Method is not working since Android Version 4.
How can I disable the homebutton?
EDIT: Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.home" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<permission android:name="android.permission.WRITE_SECURE_SETTINGS" >
</permission>
<application
android:icon="#drawable/ic_launcher_home"
android:label="#string/home_title" >
<service android:name=".MyService" >
</service>
<activity
android:name="Home"
android:launchMode="singleInstance"
android:stateNotNeeded="true"
android:theme="#style/Theme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<receiver android:name=".ScreenReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</activity>
<activity
android:name="Wallpaper"
android:icon="#drawable/bg_android_icon"
android:label="Wallpaper" >
<intent-filter>
<action android:name="android.intent.action.SET_WALLPAPER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".LockPage" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
This may come a bit late, I have been on a somewhat similar situation. My problem was that I didn't want users to leave the call screen during phone calls but I couldn't prevent it, so instead I simply brought it back front every time they left it.
In your case you could simply bring your app back to front on pause:
#Override
protected void onPause() {
super.onPause();
// Close and reopen app or bringToFront()
}
So leaving would automatically open the app again. You should try either reopening your activity or bringing it to front and see what works best. Reopening may be unnoticeable if you remove all animations and add FLAG_ACTIVITY_NO_ANIMATION.
It seems like the Home Button press is not forwarded to the app in a homescreen application.
Therefore I made a normal app, put my broadcastReceiver and my service in and now I can disable the homebutton and the backbutton.
Still the recent Apps button can be used to jump out of my lockscreen. You can flood it with dummy entries which might work.
Hope that helps someone!
#Override
public void onAttachedToWindow()
{
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
This is not possible without changing the Android source: [Mentioned here][1].
Also this would break the Android Activity Cycle, which is not recommended.
this code works in my application
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}

Categories

Resources