afo*_*eca 19 android android-sdk-2.1
在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.intent.action.MAIN cat = [android.intent.category.HOME] flg = 0x10200000}键入null
04-06 02:34:42.379:INFO/ActivityManager(1017):开始活动:意图{act = android.intent.action.MAIN cat = [android.intent.category.HOME] flg = 0x10200000 cmp = android/com. android.internal.app.ResolverActivity}
有谁知道这第一条日志消息是什么意思?也许我没有正确使用API,任何想法?任何帮助将不胜感激.
a2r*_*nus 11
@afonseca:我正在处理同样的问题.感谢您的代码,我用它来开始.还要感谢Shimon.我将他的答案合并到了我的脑海里.我已经有了代码工作(1.6和2.1更新1).它已经调整了一点,但2个主要变化似乎是Shimons的建议和:".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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32522 次 |
最近记录: |