I have an Launcher app installed. And user has choose another launcher as default, that means when pressing HOME the default launcher will come to front.
I wanna supply user with an convenience of reseting default launcher. Such as a button clicking in my launcher's UI will make Launcher-Pick-Up popup window showing.
Go Launcher can do that(in Go's setting view). it seems Go Launcher does something like "PackageManager.clearPackagePreferredActivities("com.android.launcher")" to clear the prefered launcher activity!
how to achive that ?
launcher can only clear its own prefered settings for security issue.
here is a work around:
register a mockup activity in Manifest.xml:
<activity
android:name="MockupLauncher"
android:enabled="false"
android:exported="false"
android:excludeFromRecents="true"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
use "PackageManager.setComponentEnabledSetting" to trigger OS clear current prefered launcher:
private void resetPreferedLauncher() {
PackageManager pm = mContext.getPackageManager();
ComponentName mockupComponent = new ComponentName(MockupLauncher.class.getPackage().getName(), MockupLauncher.class.getName());
pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(startMain);
pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
// or
//pm.setComponentEnabledSetting(mockupComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
that is how I make it! enjoy!
来源:https://stackoverflow.com/questions/20471350/how-to-make-the-launcher-pick-up-popup-window-showing