<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter> <action>这段去掉
<activity>加上 android:alwaysRetainTaskState="true"这句
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter> <action>这段去掉
<activity>加上 android:alwaysRetainTaskState="true"这句
Intent shortcutIntent = new Intent(this, MainActivity.class);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
shortcut.putExtra("duplicate", false); //不允许重复创建
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sendBroadcast(shortcut);
}