【发布时间】:2011-10-22 17:50:14
【问题描述】:
我知道它没有记录在案,也不会在所有设备上运行,但我看到越来越多的应用在安装后将快捷方式放在主屏幕上。 找到一堆代码块如何做到这一点,但对我来说它们不适合。 这就是我现在得到的。
-
需要清单中的权限。
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> -
创建应调用的活动意图。前(来自 cgeek):
Intent shortcutIntent = new Intent(); shortcutIntent.setClassName("com.example.androidapp", "SampleIntent"); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); -
自己创建快捷方式
Intent addIntent = new Intent(); addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon)); addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); context.sendBroadcast(addIntent);
我的问题是: 安装 .apk 后,此代码应该去哪里添加快捷方式?我在启动器活动中尝试了此代码,每次应用启动时它都会创建损坏的(另一个故事)快捷方式。
【问题讨论】:
-
作为用户我也同意,真的很烦。但是...作为开发人员,如果有人提出要求并且我解释说不能这样做,但仍然坚持要我这样做。是的,你可以在 ICS 之后这样做。权限是必要的,其余的check this answer
-
这个答案不适用于 kitkat...
标签: android shortcut android-3.0-honeycomb