将android启动器快捷方式添加到主屏幕

时间:2013-06-26 20:11:18

标签: java android shortcut launcher homescreen

This person was generous enough to add his code to his question。但我无法弄清楚从哪里得到以下变量:

resapp_id和<{p}中的p

BitmapDrawable bd = (BitmapDrawable) (res.get(app_id).activityInfo.loadIcon(p).getCurrent());

这是他的代码:

Intent shortcutIntent = new Intent();
shortcutIntent.setClassName(ai.packageName, ai.name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);

BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent());
Bitmap newbit;
newbit=bd.getBitmap();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit);

intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(intent);

此外,还有什么必须添加到清单文件中吗?

编辑:

我替换

BitmapDrawable bd=(BitmapDrawable)(res.get(app_id).activityInfo.loadIcon(p).getCurrent());
Bitmap newbit;
newbit=bd.getBitmap();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, newbit);

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(this, R.drawable.launcher_icon));

它仍然不起作用。这就是为什么我想知道清单文件。

1 个答案:

答案 0 :(得分:1)

在你需要的清单中

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
相关问题