I have been working on a replacement for the stock Car Home app for a bit, and I am completely stumped on how to override the Home button so that my app will be brought back to the foreground whenever the phone is docked. This is the way that Car Home works, so there must be a way.
It seems that BroadcastReceivers won't work, because the intent that is broadcast whenever the Home button is pressed will still cause the default homescreen app to launch; I cannot prevent it. I can override the Home button from within my app, but that does me no good since this needs to work when the user is outside my app. Car Home also does not do anything weird like set itself as the default homescreen app while it's running (I checked the logcat to make sure).
What can I try next?
Well, after many months I have finally found the answer to this question. The key is the "android.dock_home" metadata element, found here:
http://developer.android.com/reference/android/content/Intent.html#METADATA_DOCK_HOME
By using this in your AndroidManifest.xml, you can make your dock application become the home application temporarily. To do this, add this line to the AndroidManifest.xml inside the Activity tags for the dock app activity:
<meta-data android:name="android.dock_home" android:value="true" />
If the value is set to true, as long as your phone is docked the Home button will return you to the dock app. After undocking, the Home button will take you back to your normal home app.
Unfortunately, there is no way in the public APIs to override the Home button without the user confirming it.
Your best bet would be to implement a CATEGORY_HOME Intent. This means when a user pressed Home they would be presented with the option to run the standard Home or yours and make yours the default if they wanted.
When your application was launched you could then check if the phone was docked. If the phone is not docked you could then open the standard Home screen and close your app before anything is displayed.
You need to the correct intent filter in your manifest for the app to launch automatically when you dock the phone. Refer to http://developer.android.com/reference/android/content/Intent.html#CATEGORY_CAR_DOCK for the information.
I found a way to tackle HOME key. For your application set the manifest as
<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"/>
Now your application is an alternate Launcher application.
Use the adb, and disable the launcher application using package manager
pm disable com.android.launcher2.
Now the Home key press will aways stay in the same screen.
Related
I am writing Android application for kiosk mode. I am using this tutorial to create kiosk mode: http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/
However, in the tutorial, the user still can click on home and then the application back after 2 seconds.
So, I did a bit of modification to disable the home button by making my application as a home. I did it by put this in my manifest:
<activity android:name=".MainActivity"
android:launchMode="singleInstance">
<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>
Everything work well. But when the user try to exit (ie. user login as administrator), my application is back again. I suspect because I set it as HOME.
My question is, how to allow my app to Exit. Is it possible to go back to actual home when my app exit? If not, is there a better way to tackle this home problem (ie. disable home button and not actually set it as home)?
You have multiple HOME screens installed (the default one provided by the device manufacturer, and your app). The user must have chosen that your app should be the default HOME screen (this usually happens at boot time). What you now want to do is to remove this "preferred" setting so that the user can choose a different "default" HOME screen (ie: the manufacturer's app). Do that like this:
PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities ("your.package.name");
and then finish() your MainActivity.
EDIT: Alternative solution
As an alternative solution, when you want to "exit" your app, you just launch the default HOME screen instead. To do this, you need to either know the package and class name of the default HOME screen and hardcode that, or you can scan for that info using PackageManager like this:
PackageManager pm = getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> infoList = pm.queryIntentActivities(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
// Scan the list to find the first match that isn't my own app
for (ResolveInfo info : infoList) {
if (!"my.package.name".equals(info.activityInfo.packageName)) {
// This is the first match that isn't my package, so copy the
// package and class names into to the HOME Intent
homeIntent.setClassName(info.activityInfo.packageName,
info.activityInfo.name);
break;
}
}
// Launch the default HOME screen
startActivity(homeIntent);
finish();
In this case, your app is still set as the default HOME screen, so if the user presses the HOME key again, your app will be started. But the user can then "exit" your app to return again to the original HOME screen.
You can use the device owner capabilities introduced in Android 5.0 to fully manage an Android device and use it as a kiosk. Among other things this allows you to prevent the user from exiting the app by tapping the home button.
The simplest way to set up a device owner kiosk is to use the Android Management API and configure a kiosk policy.
I am developing an app which needs to receive/intercept event of long pressed home button. I believe this function can be implemented by broadcast receiver intent filter because an existing app called "Home Button Launcher (https://play.google.com/store/apps/details?id=com.dynamicg.homebuttonlauncher&hl=en)" but I can't find any relevant discussion about how to do it.
When this app is installed and the home button is pressed for a long time (3~5 secs), Android ask users to choose which application need to be launched (either the default Google search or the installed Home button launcher), as shown in https://play.google.com/store/apps/details?id=com.dynamicg.homebuttonlauncher&hl=en
There are many posts discuss about how to get home button event in applications for knowing when their apps are closed by users but there is no post about how to get this broadcasted event in service.
Does anyone know how I can receive/intercept this event as what Home button launcher does?
Thank for the remind posted by Selvin. The only thing is to add this intent filter in manifest file:
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
I am building an app that will form part of an exhibition. It will be displayed on a Nexus 7 which will be securely mounted. The app has touchscreen functionality and will display interactive content.
I need to be able to disable as many features as possible whilst on display as I do not want the public to be able to get to anything other than the app.
The main thing I am struggling with is the back/home/recent app list button. I have found some examples of disabling home button (child lock Android - Is It possible to disable the click of home button
) but ideally I need the buttons to be invisible, so to turn off the 'glow' (black would be fine).
Is the bottom section on a Nexus 7 protected in some way, is there another version of Android that would allow me to do this? The Nexus device will only be used for displaying this app, no other functionality is needed.
Any suggestions would be great and very much appreciated.
Your best solution without creating your own custom Android rom to remove the bottom buttons, will be to make the app full screen, override the back button, and make your app a launcher in order to override the home button.
AFAIK, there is no way of overriding the recent apps button.
Edit: One other option would to have a fullscreen app and then use a mount that will cover the buttons. (Thanks to MaciejGórski for the idea).
To make your app full screen, put the following in your activity's onCreate():
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Or you can make the app full screen from within the manifest as well, thanks to #Niels:
<application android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
To override the back button, add this method:
#Override
public void onBackPressed() {
return;
}
Now the home button is trickier, add the following to your manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
and this to your manifest under the <activity>:
<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>
and this to your manifest under the <application>, make sure that the <receiver name> is the full package name path you define:
<receiver android:name="com.example.BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And lastly, create a java class file called BootCompleteReceiver, and use this code:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startActivityIntent = new Intent(context, YourActivityName.class);
startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(parentActivityIntent);
}
}
To later disable your app as a home screen launcher, press the recent app button, swipe down from the right side, tap settings, go to apps, then tap the upper right three dots (vertically aligned), press "Reset app preferences", and then finally press "Reset apps".
I think that should just about cover it all.
EDIT 2 I just realized/tested and you do NOT necessarily need the BOOT_COMPLETED intent if you make your application a launcher. This means that the <uses-permission>, <receiver>, and BootComplete.java are not needed. You can just use the <intent-filter> that includes the MAIN, HOME, and DEFAULT attributes.
EDIT 3 More/different information available here: Home Launcher issue with Fragments after reboot
Further to the above, which all worked great, and to make sure a comprehensive answer is out there.....
AFAIK, there is no way of overriding the recent apps button.
I got around this by change onPause app behavior to start an alarmmanager. There may be a more elegant solution, but this works.
First, create repeating alarmmanager setupAlarm(seconds)( full details here and here, note I used repeating alarm rather than one off, think both will work though) that starts your activity
then change onPause to set a 2 second alarm, so whenever someone selects the recent apps button on the nav bar, a 2 second 'alarm' to start mainActivity is set.
#Override
public void onPause() {
setupAlarm(2);
finish(); //optional
super.onPause();
}
So with this and the above, any attempt to use the navigation buttons or restart the app results in app starting. So until I get round to investigating the 'kiosk' style roms this is a very good compromise.
I may be a bit late.
But i've found the, in my opinion, best solution for the Recent Apps Button Problem:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (&& !hasFocus) {
// Close every kind of system dialog
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
// send task back to front
ActivityManager activityManager =
(ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);
}
}
The "send task back to front" part will prevent the pull down of the Notification bar by simply sending it back up instantly and will close the Recent Apps View.
The other one is to close the "Shutdown/Restart" View when he tries to shut down his phone.
Now Excuse my English and have a nice Day.
Greetings
Jimmy
Hello my question is a bit difficult let me explain and try to understood
I have a custom Home who is set as default when user press home. Know i need a way to ask wich home use when i press on a setting key, or a way to launch Setting activity for delete home default action.
In fact i wan't a way to go back to home app installed by google instead of mine just in case.
I need this because my custom home is still in dev and don't have all feature yet.
I need to keep my home as a default action when user press home buton but i need to add a home selector in special case.
I have try to use finish() when i need to go back to old home but mine as default is automaticly relaunch because he is the default one.
Any idea?
EDIT
I try to reformul a bit.
I'm making a home apps. I set it as default on my tablet. My home apps don't have all feature yet. And when i will launch something or go to setting i need to relaunch default home app.
So i need a way inside my app to reset default home action or launch a home apps, or ask user to choice wich app he will use has home apps.
Hope this is clear for all.
Probably you should use this one to implement your idea. keep the below lines in your manifest file.
<activity android:name="Home"
android:theme="#style/Theme"
android:launchMode="singleInstance"
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" />
</intent-filter>
</activity>
For more info about the sample project. just go the below path in your Android SDK
\android-sdk\samples\android-8\Home
EDIT :
Hi Please check this to pro grammatically unset the default application.
How do I use PackageManager.addPreferredActivity()?
for more info go through this also to clear the clearPackagePreferredActivities
http://developer.android.com/reference/android/content/pm/PackageManager.html#clearPackagePreferredActivities%28java.lang.String%29
hope this helps you.
How to disable the home key when the menu or Alarm Dialog pop up?
I know how to disable the home key in the activity. But when the menu or Alarm Dialog pop up, the way is invalid. How to implement this function. Or is there no way to deal with this case?
You can not disable Home Key . for this is the last chance the Android System ESCAPE the application.
I had the same requirement in my project. Here is my solution:
1) You should have a rooted device.
2) Make sure that your activity is main activity, it should have params like these:
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
If you have it, when you tap home button android suggest you other apps except Launcher...
3) Next, since we have rooted device, just find and rename Launcher.apk to LauncerBBB.apk(it can be done programmatically even).
4) That's all. Now only your app is main and it will be shown by Home key tap.
P.S. If you need to have an access to Launcher you can start LauncerBBB activity from secret menu of your application.