Load launcher app again?

删除回忆录丶 提交于 2019-12-11 03:01:22

问题


I have a custom launcher. When I install it, I open it for the first time and use the following code to load the intent chooser for the launchers:

Intent localIntent = new Intent(Intent.ACTION_MAIN);
localIntent.addCategory(Intent.CATEGORY_HOME);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localIntent);

My app will show the all the launchers. Then I choose the other launcher, the one that's notm y launcher, and set it to default. Then the next time the code above runs, the intent chooser does not appear and I cannot change the default launcher. Here is what I've tried to fix it:

private void callIntenChoiceLauncher(Context c) 
{
    PackageManager p = getPackageManager();
    ComponentName cN = new ComponentName(c, RunDefaultLauncher.class);
    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);        
    c.startActivity(selector);

    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

It's not working.


回答1:


In this code:

c.startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

when you call startActivity(), the activity isn't started immediately. What this does is just tell the Android framework that you would like to start another activity at the next possible time that the framework gets control. In the next line you disable the component so that when the Android framework goes to start the activity, your launcher component has been disabled again.

The call to startActivity() is not synchronous.




回答2:


Assuming you want to disable your home activity after the chooser has been shown so that the default launcher will react to the home button until you enable your activity:

Do not disable your home activity after startActivity. This should be done in the onDestroy() method of your home activity.

Modify onCreate() of your home activity:

public void onCreate(Bundle savedInstanceState) {
    /*check some flag in sharedPreferences or elsewhere 
    to determine if the activity was launched as part of the chooser process
    */
    finish();
}

Add onDestroy() to your home activity:

public void onDestroy() {
    //Disable the activity again
    PackageManager p = getPackageManager();
    ComponentName cN = new ComponentName(c, RunDefaultLauncher.class);
    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

If you do not want to disable your home activity after the chooser was shown, simply do not call p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);



来源:https://stackoverflow.com/questions/18697253/load-launcher-app-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!