Android Overriding Home Key

Can I override the 'Home' button in my application?

This answer will no longer work, not since Android 4.0.

The correct solution is to create an app that can intercept the Home intent, as per @bara's answer below.


You can override the home button as any other button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
return true;
}
return super.onKeyDown(keyCode, event);
}

Overriding the functionality of Home Button

Try this:

@Override
protected void onUserLeaveHint() {
if (!navigating) {
Intent intent2 = new Intent();
intent2.setClass(ActivityA.this, ActivityB.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
forceHome(this, intent2);
}
super.onUserLeaveHint();
}

public static void forceHome(Context paramContext, Intent paramIntent) {

if (paramIntent != null) {
((AlarmManager) paramContext.getSystemService(ALARM)).set(1,
System.currentTimeMillis(),
PendingIntent.getActivity(paramContext, 0, paramIntent, 0));
}

}

As a security feature of android, the activity only launches after 5 seconds. If you want to launch it immediately. use your own Home Launcher.

Android Overriding home key

I agree with @Romain Guy

You can't override the behaviour of home button.

What the Car Home app does: it has defined itself as a launcher. You can also define yours as a launcher, and it will be notified when the home screen is about to be launched.

Check this out : Intent.html#CATEGORY_LAUNCHER

Please note that this doesn't mean that your app/activity is notified when the home button is pressed so that it can override the behaviour of it, but is notified when the system is about to launch the Home Screen. Both are different things. in this type of notification, Android already has sent the app (Which is currently executing) to the frozen state, and it won't have any control over what's happening.

This is as far as I know. I guess @Romain Guy may correct me if I am wrong at any place.

Overriding the Home button - how do I get rid of the choice?

You can't permanently override the Home button without the user confirming it.

One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hijacked a user's phone.

Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.

Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.

override Home key in android ICS

Post ICS i.e. Android 4+, the overriding of the HomeButton has been removed for security reasons, to enable the user exit in case the application turns out to be a malware.

Plus, it is not a really good practice to not let the user navigate away from the application. But, if your application requires so, what you can do is declare the activity as a Launcher , so that when the HomeButton is pressed it will simply restart your application and remain there itself (the users would notice nothing but a slight flicker in the screen).

EDIT #1 : Here is another workaround, more suited to your needs.

EDIT #2 : Just came across this. Haven't tested it. But looks kinda promising. Not sure if it would work, but you could give it a try.

Override Home Button Android

You can make your application as a Home screen launcher app using very similar activity code in your AndroidManifest. This way when user press the home button your app will be opened.

    <activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- The following two intent-filters are the key to set homescreen -->
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

</intent-filter>
</activity>


Related Topics



Leave a reply



Submit