问题
I am trying to launch the Home-screen/Launcher chooser dialog programmatically by using the following intent:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(i, "Set My HomeScreen as default"));
But unfortunately the dialog that appears with the list of installed home-screen launchers does not have the Use by default for this action option at the bottom of the dialog. The following image shows how it looks:
![](https://www.eimg.top/images/2020/03/17/83fc1ad4c9f827cb1814326146071055.png)
Interestingly after choosing my home-screen from the above chooser dialog, if I press home button from my that screen then Android automatically shows up a similar dialog which in fact has the Use by default for this option at the bottom of the dialog. Here is how it looks:
![](https://www.eimg.top/images/2020/03/17/6d3d3aa748683d34a1fca3933a2546ef.png)
I am pretty clueless about what's wrong with the above code, it must be some silly mistake that I am not able to spot myself.
if anyone of can shed some light, then it would be of great help.
Thanks
回答1:
createChooser()
does not produce a "default for this action" checkbox. If you'd like the checkbox, instead pass an intent to startActivityForResult()
回答2:
This is how you do it on ICS:
final PackageManager packageManager = this.getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
startActivity(selector);
来源:https://stackoverflow.com/questions/10709779/android-home-screen-launcher-chooser-does-not-show-use-by-default-for-this-acti