相关疑难解决方法(0)

我如何使用PackageManager.addPreferredActivity()?

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

一些必要的背景.我正在写一个锁屏替换应用程序,所以我希望主键启动我的应用程序(已经运行,因此具有禁用密钥的效果).当用户"解锁"屏幕时,我打算恢复映射,以便一切正常.

在我的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>
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我有以下代码段:

// 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);
Run Code Online (Sandbox Code Playgroud)

由此产生的行为是,当我按下Home键时,应用程序选择器出现,这表示clearPackagePreferredActivities()调用有效但我的应用程序没有被添加为首选.此外,下面日志中的第一行说明了"删除Intent的首选活动":

04-06 02:34:42.379:INFO/PackageManager(1017):结果集已更改,丢弃Intent的首选活动{act = …

android android-sdk-2.1

19
推荐指数
1
解决办法
3万
查看次数

在Android中使用PackageManager的目的

什么是PackageManager?

  • 使用它的目的是什么?

以下方法描述了什么?:

  1. getPackageManager()
  2. getApplicationInfo()
  3. getPackageName()

任何人都可以提供详细说明,因为我无法在Android开发人员指南中获得完整信息.

还有一个包含所有方法的示例程序.

android

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×2

android-sdk-2.1 ×1