Not able disable Home button on specific android devices

后端 未结 3 1195
春和景丽
春和景丽 2020-11-27 06:29

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 s

相关标签:
3条回答
  • 2020-11-27 07:17

    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.

    0 讨论(0)
  • 2020-11-27 07:22

    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.

    0 讨论(0)
  • 2020-11-27 07:33

    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" />
    
    0 讨论(0)
提交回复
热议问题