Not able disable Home button on specific android devices
Asked Answered
S

3

4

I know this question has been asked many times and the answer is always "No we cant disable home button".

I have a little different query to ask.

I wrote simple code in which my activity overrides the onKeyDown() and return true for all key presses.

In theory this means whoever opens the application is stuck there and has no option to move out of the application.

When i tested this application on different devices, i made following observations :

  1. On motorola device with OS as 2.2.2 , Home button got disabled.
  2. On HTC device with OS as 2.3.5 , Home button got disabled.
  3. On Sony with OS as 2.3.7 , Home button got disabled.
  4. On Samsung with OS as 2.2.1 and 2.3.3 , Home button got disabled.
  5. On Samsung with OS as 2.3.6 and 4.0.4, Home button remained enabled.

These observations are seems very conflicting.

Does any one have any idea , why different devices are behaving differently and what is the best way to handle such scenario.

As per my understanding till now none of the vendors have customized Android OS . Everyone is putting there UI layer on top of it but no one has touched the internals.

Stimulus answered 27/4, 2012 at 11:21 Comment(0)
S
0

As mentioned in my question itself for devices below 2.3.6 OS overriding keypress() functions work well.

Issue starts with 2.3.6 onwards. I don't know what these device vendors have done but keypress() function does not functions same on all devices.

Also from ICS onwards google has stopped using keypress() function once for all.

So how do we do it.

The way i see it, if we are trying to override home button then its not possible, but definitely we can listen to it.

In our android manifest we use <category android:name="android.intent.category.HOME" /> filter than this makes our activity as home replacement screen. Now when you will press home button the content resolver pop up will always come up and ask which application i.e the default launcher or your application should respond to home button click. You can always choose your application there.

But this does not overrides or disables home button. whenever you will press home button same thing will be repeated again and again till you make your application default , by clicking the use as default checkbox given in the content resolver pop up.

Now once you have chosen your application as default home press will always launch your application.

Done... no. The issue which arises know is if you move out of your application the home button still launches your application. How to get rid of it once your work is done.

What we have to do is while closing or calling finish() on our activity , prior to it we should set the package setting to default by using:

paramPackageManager1.setComponentEnabledSetting(localComponentName2, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 1);

This will de associate the home button from your activity.

Stimulus answered 10/5, 2012 at 9:18 Comment(1)
I think that part of the "disabling the home button" would be to prevent the "Complete action using" dialog. If user will choose the default android launcher in the dialog you will not be able to handle the home button anymore.Braeunig
D
7

I know this question has been asked many times and the answer is always "No we cant disable home button".

If you want to handle the HOME button, implement a home screen.

Does any one have any idea , why different devices are behaving differently

Because they are different devices, and the vendors made changes. Also, in the case of 4.0.4, additional protections may have been added, to help prevent malware authors from hijacking the HOME button without being a home screen.

what is the best way to handle such scenario

If you want to handle the HOME button, implement a home screen.

Everyone is putting there UI layer on top of it but no one has touched the internals.

This is incorrect. Pretty much every device vendor has "touched the internals", to varying degrees. So long as they meet the compatibility requirements for the Play Store, their changes are deemed acceptable by Google.

Disincline answered 27/4, 2012 at 11:57 Comment(4)
hi @Disincline , i agree with everything which u have said. but if this is so, then why certain applications like Toddler lock are able to disable the home screen button without converting their app into home screen application ??. Am also able to disable home button by implementing the home screen, but the prompt which comes to select an application to complete the action causes issue. If i select my application then its ok, but if i choose anyother application than home button starts working in my application. Toddler lock has somehow able to bypass this functionality...Stimulus
@dexprab: Toddler Lock is a home screen, last I checked. If it has the functionality you describe, then the security holes that Toddler Lock exploits need to be closed.Disincline
i dont think there is any security hole, as the app has a efficient pattern based log out mechanism. also its listed as one of the best apps on many sites. I guess google might have looked into it if it was a security glitch. Even if it is i would really be insterested in knowing how did they managed to do it. I have their manifest with me but its not helping me much in achieving the functionalityStimulus
@dexprab: You are welcome to your opinion. One day, if malware takes over your device and prevents you from leaving the malware because it took over the HOME button, you will change your mind. I am trying to help prevent that sort of malware.Disincline
I
6

You may want to give this a try:

@Override
public void onBackPressed() {

}

@Override
protected void onUserLeaveHint() {
    super.onUserLeaveHint();
    ((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);
}

@Override
protected void onPause() {
    super.onPause();
    ((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)).moveTaskToFront(getTaskId(), 0);

}

Permissions needed -- add the following to manifest

<uses-permission android:name="android.permission.REORDER_TASKS" />
Iq answered 4/10, 2015 at 22:19 Comment(1)
It is to be added the correct answer after being a launcher App. Disables Back button, taskmanager button and the notification bar interaction by going back to the acitivity whenever the user leaves itIq
S
0

As mentioned in my question itself for devices below 2.3.6 OS overriding keypress() functions work well.

Issue starts with 2.3.6 onwards. I don't know what these device vendors have done but keypress() function does not functions same on all devices.

Also from ICS onwards google has stopped using keypress() function once for all.

So how do we do it.

The way i see it, if we are trying to override home button then its not possible, but definitely we can listen to it.

In our android manifest we use <category android:name="android.intent.category.HOME" /> filter than this makes our activity as home replacement screen. Now when you will press home button the content resolver pop up will always come up and ask which application i.e the default launcher or your application should respond to home button click. You can always choose your application there.

But this does not overrides or disables home button. whenever you will press home button same thing will be repeated again and again till you make your application default , by clicking the use as default checkbox given in the content resolver pop up.

Now once you have chosen your application as default home press will always launch your application.

Done... no. The issue which arises know is if you move out of your application the home button still launches your application. How to get rid of it once your work is done.

What we have to do is while closing or calling finish() on our activity , prior to it we should set the package setting to default by using:

paramPackageManager1.setComponentEnabledSetting(localComponentName2, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 1);

This will de associate the home button from your activity.

Stimulus answered 10/5, 2012 at 9:18 Comment(1)
I think that part of the "disabling the home button" would be to prevent the "Complete action using" dialog. If user will choose the default android launcher in the dialog you will not be able to handle the home button anymore.Braeunig

© 2022 - 2024 — McMap. All rights reserved.