我需要添加我的Android应用程序到主屏幕作为快捷方式编程。
请给出这方面的想法。如果可能,请告诉我如何管理现有的快捷方式(删除和添加更多的快捷方式)。
发布于 2010-07-10 12:43:10
发布于 2014-10-17 16:38:20
我花了相当多的时间尝试来自stackoverflow的不同解决方案,但大多数都是无用的,因为它们正在启动新的活动实例。我需要一个快捷键,它的工作方式完全像应用程序列表中的一个或Google Play自动安装的(启动活动或将已经开始的活动带到前面)。
@Override
public void onCreate(Bundle savedInstanceState) {
//Save the flag to SharedPreferences to prevent duplicated shortcuts
if (!settings.isShortcutAdded()) {
addShortcut();
settings.setShortcutAdded(true);
}
}
private void addShortcut() {
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
shortcutIntent.addFlags(flags);
Intent addIntent = new Intent();
addIntent.putExtra("duplicate", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
.fromContext(getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
别忘了更新你的清单:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
https://stackoverflow.com/questions/3217741
复制相似问题