Comment créer un bouton qui place n icônes sur l’écran d’accueil, ces icône lançant l’application avec des paramètres distincts ?
Passage et récupération de paramètres :
Lors de la création de l’icône :
shortcutIntent.putExtra("parameterName", "parameterValue");
Au lancement de l’application :
Intent addIntent= getIntent(); // gets the previously created intent
String parameter = addIntent.getStringExtra("parameterName"); // will return "parameterValue"
Fichier manifest :
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Création d’icône :
// -- shorcutIntent object
Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
// -- shortcutIntent is added with addIntent
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcutIntent.putExtra("contenu", sVar); // -- ("parameterName", "parameterValue")
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, sVar);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.alien));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// -- finally broadcast the new Intent
getApplicationContext().sendBroadcast(addIntent);
Suppression d’icône :
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "1St");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
Exemple :
ici on aura la possibilité de supprimer la première icone créée, les autres seront créées avec un nom et un paramètre aléatoire. A télécharger ici
Références :
http://androidforbegineers.blogspot.in/2014/01/android-home-screen-shortcut.html#
https://codinggeekorg.wordpress.com/2011/01/02/android-how-to-add-home-screen-shortcuts-to-your-app/
http://stackoverflow.com/questions/6988511/how-to-add-apps-shortcut-to-the-home-screen
http://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen