he pasado un montón de tiempo en probar diferentes soluciones de stackoverflow, pero la mayoría de ellos son inútiles, porque están comenzando nuevas instancias de actividad. Necesito un acceso directo que funcione exactamente como el que está en la lista de aplicaciones o el que se instala automáticamente en Google Play (inicie la actividad o lleve la Actividad ya iniciada al frente).
@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);
}
Y no se olvide de actualizar su manifiesto:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
no creo que los apis exponen a esa funcionalidad. Obtienes un icono de aplicación en el cuadro de diálogo desplegable de forma predeterminada. –