In SDK 1.5 I was using the PackageManager class to set the preferred home screen to be my app using PackageManager.addPackageToPreferred(). In the new SDK (using 2.1) this has been deprecated so I'm trying to use addPreferredActivity() for the same result but it's not working as expected.
Some necessary background. I'm writing a lock screen replacement app so I want the home key to launch my app (which will already be running, hence having the effect of disabling the key). When the user "unlocks" the screen I intend to restore the mapping so everything works as normal.
In my AndroidManifest.xml I have:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<uses-permission android:name="android.permission.SET_PREFERRED_APPLICATIONS">
</uses-permission>
In my code I have the following snippet:
// Set as home activity
// This is done so we can appear to disable the Home key.
PackageManager pm = getPackageManager();
//pm.addPackageToPreferred(getPackageName());
IntentFilter filter = new IntentFilter("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
ComponentName[] components = new ComponentName[]
{
new ComponentName("com.android.launcher", ".Launcher")
};
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(),
MyApp.class.getName());
pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY,
components, component);
The resulting behavior is that the app chooser comes up when I press the Home key, which indicates that the clearPackagePreferredActivities() call worked but my app did not get added as the preferred. Also, the first line in the log below says something about "dropping preferred activity for Intent":
04-06 02:34:42.379: INFO/PackageManager(1017): Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 } type null
04-06 02:34:42.379: INFO/ActivityManager(1017): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=android/com.android.internal.app.ResolverActivity }
Does anyone know what this first log message means? Maybe I'm not using the API correctly, any ideas? Any help would be greatly appreciated.