How do I use PackageManager.addPreferredActivity()? How do I use PackageManager.addPreferredActivity()? android android

How do I use PackageManager.addPreferredActivity()?


@afonseca: I was dealing with the same problem. Thanks for the code, I used it to start with. Also thanks Shimon. I merged his answer into mine. I've got the code working (on 1.6 and 2.1 update 1). It has been adjusted a little bit, but the 2 main change seem to be Shimons suggestion and: ".Launcher" was changed to "com.android.launcher.Launcher". The working code is posted below.

Ciao, a2ronus

PackageManager pm = getPackageManager();IntentFilter filter = new IntentFilter();filter.addAction("android.intent.action.MAIN");filter.addCategory("android.intent.category.HOME");filter.addCategory("android.intent.category.DEFAULT");Context context = getApplicationContext();ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};pm.clearPackagePreferredActivities("com.android.launcher");pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);


This answer may come a little late but API docs says for clearPackagePreferredActivities:

An application can only clear its own package(s).

So, I think that in "restoring the mapping" the only you can do is something like:

getPackageManager().clearPackagePreferredActivities(getPackageName());

and so clearing the default setting for HOME screen.


This seems to work for me if I initialize the components array to ALL HOME apps on the device:

ComponentName[] components = new ComponentName[]{   new ComponentName("com.intuitiveui.android", "com.intuitiveui.android.Friday"),   new ComponentName("com.android.launcher2","com.android.launcher2.Launcher")};

My problem is how do I fill this dynamically.