【发布时间】:2013-04-23 06:49:33
【问题描述】:
我编写了以下代码,用于在主屏幕中创建应用程序的快捷方式:
private void createShortcut() {
Intent shortcutIntent = new Intent(this, ActActivation.class);
shortcutIntent.setClassName("org.mabna.order",
"org.mabna.order.ui.ActActivation");
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, "test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
this.sendBroadcast(addIntent);
}
它可以正常工作,但是每次我运行我的程序并运行此代码时,我都会收到“快捷方式测试已创建”消息,并且我的主屏幕上会添加一个新快捷方式。打开我的应用程序 10 次后,我有 10 个快捷方式。
如何防止出现此消息框并创建多个快捷方式?
【问题讨论】:
-
在
SharePreferences中设置boolean并在每次运行时检查。在第一次运行时切换到true。我能想到的最简单的事情。 -
@IceMAN 如果用户清除应用程序数据,则重新创建快捷方式
-
@breceivemail 看看我的回答,我遇到了示例问题,但我已经解决了这个问题。
标签: android shortcut homescreen