Remove Activity as Default Launcher

后端 未结 4 587
感情败类
感情败类 2020-12-25 08:47

I set my activity as a default launcher to intercept home button clicks like so:



        
相关标签:
4条回答
  • 2020-12-25 09:29

    Have a look at the android.permission.SET_PREFERRED_APPLICATIONS permission. Also this method http://developer.android.com/reference/android/content/pm/PackageManager.html#clearPackagePreferredActivities(java.lang.String)

    0 讨论(0)
  • 2020-12-25 09:32

    if it's your app that you're clearing its defaults , you can simply call :

    getPackageManager().clearPackagePreferredActivities(getPackageName());
    

    then , in order to show the dialog of choosing which launcher to use , use:

    final Intent intent=new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-25 09:33

    You cannot override the behavior of the home key to suit your application; this is a design decision by Google, to ensure the user can always return to a static location. There may be some ways around this (if they still exist) but they are unintended bugs which an application should not rely on.

    The short answer: you can have any key except the home key.

    0 讨论(0)
  • 2020-12-25 09:35

    This solution is a clever way of doing it: Clearing and setting the default home application

    The code in onResume() basically goes like this:

        ComponentName componentName = new ComponentName(MyActivity.this, FakeHome.class);
        if (!isMyLauncherDefault()) {
            Log.e(TAG, "MyActivity is not default home activity!");
    
            // toggle fake activity
            PackageManager pm = getPackageManager();
            int flag = ((pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
            pm.setComponentEnabledSetting(componentName, flag, PackageManager.DONT_KILL_APP);
    
            // start home activity to enable chooser
            Intent selector = new Intent(Intent.ACTION_MAIN);
            selector.addCategory(Intent.CATEGORY_HOME);
            startActivity(selector);
        }
    

    and the method isMyLauncherDefault() is taken from here: How to check if my application is the default launcher

    0 讨论(0)
提交回复
热议问题