0

I tried this code and added create shortcut permission in the manifest but still not able to create the shortcut. The code is in the main activity.

//set the shortcut of the application if(!getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).getBoolean("IS_ICON_CREATED", false)) { setIcon(); getSharedPreferences("APP_PREFERENCE", Activity.MODE_PRIVATE).edit().putBoolean("IS_ICON_CREATED", true).commit(); Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();

        }
arthur kay
  • 116
  • 7

2 Answers2

0

Android provide us an intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.

First we need to add permission INSTALL_SHORTCUT to android manifest xml.

<uses-permission
        android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

The addShortcut() method create a new shortcut on Home screen.

private void addShortcut() {
    //Adding shortcut for MainActivity 
    //on Home screen
    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, "HelloWorldShortcut");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
    getApplicationContext().sendBroadcast(addIntent);
}

Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as EXTRA_SHORTCUT_INTENT.

Finally we broadcast the new intent. This adds a shortcut with name mentioned as EXTRA_SHORTCUT_NAME and icon defined by EXTRA_SHORTCUT_ICON_RESOURCE.

Also put this code to avoid multiple shortcuts :

if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
          addShortcut();
          getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
      }
Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45
  • The code is from this older question https://stackoverflow.com/questions/6337431/android-create-shortcuts-on-the-home-screen – arthur kay Feb 12 '18 at 22:36
0

I ended up using this code to create the shortcut . And it works fine it also checks snd save is preferences if the shortcut is created . Added the intent in the manifest for the receiving activity same as creator the splashscreen.

<activity android:name=".ShortCutActivity" 
android:label="@string/shortcut_label"> 
<intent-filter> 

    <action android:name="android.intent.action.CREATE_SHORTCUT" />
     <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     </activity>

Then used this code to create the shortcut

private void 

    createShortcutIcon() {
            // Checking if ShortCut was already added

            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

            boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(
                    PREF_KEY_SHORTCUT_ADDED, false);

            if (shortCutWasAlreadyAdded)
                return;

            Intent shortcutIntent = new Intent(getApplicationContext(),
                    SplashScreen.class);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
            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);

            // Remembering that ShortCut was already added
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
            editor.commit();
        }
arthur kay
  • 116
  • 7