如何使用PackageManager.addPreferredActivity()?

19
在SDK 1.5中,我使用PackageManager类通过PackageManager.addPackageToPreferred()方法将我的应用程序设置为首选主屏幕。在新的SDK(使用2.1)中,该方法已被弃用,因此我尝试使用addPreferredActivity()方法实现相同的结果,但是它没有按预期工作。
一些必要的背景。我正在编写一个锁屏替换应用程序,因此我希望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,有什么想法吗?任何帮助都将不胜感激。


问题描述得非常详细,希望你能得到你想要的答案。欢迎来到 Stack Overflow。 - Jim Blackler
4个回答

11

@afonseca:我也遇到了同样的问题。感谢您提供的代码,我用它开始了我的工作。还要感谢Shimon。我将他的答案合并到了我的里面。我已经让代码运行起来了(在1.6和2.1更新1上)。它进行了一些调整,但似乎主要变化是Shimon的建议和:““.Launcher”被更改为“com.android.launcher.Launcher”。下面是可行的代码。

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);

3
要动态获取已安装的Home应用程序列表,您可以从PackageManager.queryIntentActivities(android.content.Intent, int)开始。祝好运,a2ronus。 - a2ronus
谢谢你们俩,回答得非常好。我希望 Android 文档中有更多的示例可供参考。 - afonseca
感谢Matt和a2ronus。非常有用。 - Airsource Ltd
根据Android文档,“addPreferredActivity”已被弃用。 - Jaime Botero

4
这个答案可能有点晚了,但 API 文档上说对于 clearPackagePreferredActivities 方法:

应用程序只能清除自己的包。

所以,在“恢复映射”方面,我认为你唯一可以做的就是类似这样的操作:

getPackageManager().clearPackagePreferredActivities(getPackageName());

清除默认的主屏幕设置。

2

如果我将components数组初始化为设备上的所有ALL HOME应用程序,则似乎对我有用:

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

我的问题是如何动态填充这个内容。

1

addPreferredActivity在2.2版本中已经不再起作用,但clearPackagePreferredActivities仍然有效,但只能清除您在其上运行此操作的包的首选项。

关于这个问题,在Android Google组中有很多讨论,而Google官方目前的立场是不提供覆盖用户偏好的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接