一些必要的背景。我正在编写一个锁屏替换应用程序,因此我希望Home键启动我的应用程序(该应用程序已经运行,因此会禁用该键)。当用户“解锁”屏幕时,我打算恢复映射,以使一切正常。
在我的AndroidManifest.xml文件中,我有:
<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>
在我的代码中,我有以下代码片段:// 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);
清除应用程序偏好设置后,按Home键会弹出应用选择器,这表明clearPackagePreferredActivities()调用成功,但我的应用程序没有被添加为首选应用程序。另外,以下日志的第一行显示了“删除意图的首选活动”的内容:04-06 02:34:42.379:INFO / PackageManager(1017):结果集已更改,正在删除意图的首选活动{act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000} type null
04-06 02:34:42.379:INFO / ActivityManager(1017):启动活动:Intent {act = android.intent.action.MAIN cat = [android.intent.category.HOME] flg = 0x10200000 cmp = android / com.android.internal.app.ResolverActivity}
有人知道这个第一个日志消息的含义吗?也许我没有正确使用API,有什么想法吗?任何帮助都将不胜感激。