Spędziłem sporo czasu na próbowanie różnych rozwiązań z stackoverflow, ale większość z nich są bezużyteczne, ponieważ zaczynają nowe instancje działania. Potrzebuję skrótu, który działa dokładnie tak, jak ten na liście aplikacji lub ten, który jest automatycznie instalowany przez Google Play (rozpocznij aktywność lub przenieś już rozpoczętą aktywność do przodu).
@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);
}
I nie zapomnij, aby zaktualizować manifest:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Nie sądzę API narazić tę funkcjonalność. Domyślnie pojawia się ikona aplikacji w oknie dialogowym. –