创建应用快捷方式,且点击快捷方式回到应用,而不是重新启动应用!!

private void createShortCut() {

Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(this, this.getClass().getName());
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);

Intent intent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
getString(R.string.app_name));
// 设置快捷方式图标
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 防止重复创建
intent.putExtra("duplicate", false);

sendBroadcast(intent);

}

猜你喜欢

转载自wfkbyni.iteye.com/blog/1746565