Android 如何使用 PackageManager.addPreferredActivity()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2583966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:24:45  来源:igfitidea点击:

How do I use PackageManager.addPreferredActivity()?

androidandroid-sdk-2.1

提问by afonseca

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.

在 SDK 1.5 中,我使用 PackageManager 类使用 PackageManager.addPackageToPreferred() 将首选主屏幕设置为我的应用程序。在新的 SDK(使用 2.1)中,这已被弃用,因此我尝试使用 addPreferredActivity() 获得相同的结果,但它没有按预期工作。

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:

在我的 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>

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":

结果是当我按下 Home 键时应用程序选择器出现,这表明 clearPackagePreferredActivities() 调用有效,但我的应用程序没有被添加为首选。此外,下面日志中的第一行说的是“放弃 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 }

04-06 02:34:42.379: INFO/PackageManager(1017): 结果集已更改,放弃了 Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 } 的首选活动输入 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 }

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.

有谁知道第一条日志消息是什么意思?也许我没有正确使用 API,有什么想法吗?任何帮助将不胜感激。

采纳答案by a2ronus

@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.

@afonseca:我正在处理同样的问题。感谢您的代码,我用它开始。也感谢西蒙。我将他的答案合并到我的答案中。我的代码可以正常工作(在 1.6 和 2.1 更新 1 上)。它已经稍微调整了一下,但 2 个主要变化似乎是 Shimons 的建议,并且:“.Launcher”改为“com.android.launcher.Launcher”。工作代码发布在下面。

Ciao, a2ronus

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

回答by Paco Barter

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

这个答案可能来得有点晚,但 API 文档说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.

等清除主页屏幕的默认设置。

回答by Shimon Shnitzer

This seems to work for me if I initialize the componentsarray to ALL HOMEapps on the device:

如果我将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")
};

My problem is how do I fill this dynamically.

我的问题是如何动态填充它。

回答by mishkin

addPreferredActivity does not work anymore in 2.2, clearPackagePreferredActivities still works but you can only clear preference for the package you run this on.

addPreferredActivity 在 2.2 中不再工作,clearPackagePreferredActivities 仍然有效,但您只能清除运行它的包的首选项。

there are a lot of threads on android google groups about this problem and google's official position (for now) not to provide you with methods to overide user's preferences.

android google 组上有很多关于此问题的线程,而 google 的官方立场(目前)不会为您提供覆盖用户偏好的方法。