[ANDROID] Créer le raccourci de votre application Android sur le bureau

Android

Aujourd’hui je vous propose un petit snippet pour créer un raccourci de votre application Android sur le bureau de votre tablette ou votre smartphone.

Le code n’est pas très compliqué mais il est pratique.

Il n’est possible de créer qu’un seul shortcut Android par application.

//Fontion qui crée un shortcut sur la home
    private final void createShortcut(){
        // Get preference value to know that is it first time application is
        // being called.
        appPref = getSharedPreferences("isFirstTime", 0);
        isFirstTime = appPref.getBoolean("isFirstTime", true);
 
        if (isFirstTime) {
            // Create explicit intent which will be used to call Our application
            // when some one clicked on short cut
            Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            Intent intent = new Intent();
 
            // Create Implicit intent and assign Shortcut Application Name, Icon
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, AppConsts.SHORTCUT_TITLE);
            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
            intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            getApplicationContext().sendBroadcast(intent);
 
            // Set preference to inform that we have created shortcut on Homescreen
            SharedPreferences.Editor editor = appPref.edit();
            editor.putBoolean("isFirstTime", false);
            editor.commit();
 
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *