0

This question may sound duplicate but there is no answer that i found is working:

I have gone through these question(s):

Android create shortcuts on the home screen

But the proposed solution is not working.

I have used the below solution which is working in API Level < 23

  Intent shortcutIntent = new Intent(context,
                LedgerDetailActivity.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, ledgerBean.getName());
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
        addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
        addIntent.putExtra("duplicate", false);
        context.sendBroadcast(addIntent);

Added Permission:

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

But the above solution is not working in all devices and giving weird functionality sometimes like below:

  1. In some devices like Samsung Edge (with Android N) shortcuts are not getting created
  2. In my emulator with Android 7.1 (Android N) only one shortcut is getting created

Can someone please help me, There is no official documentation for this feature, Please don't confuse with App shortcuts introduced in Android N. I need shortcuts in home screen.

Community
  • 1
  • 1
Manikanta
  • 3,421
  • 4
  • 30
  • 51
  • Instead of starting with all the things that don't work, perhaps you should start by **explain what it is that you're trying to do**. The topic alone is hardly enough. At least one of the answers completely misunderstood you. You might want to provide a screenshot and explain the expected result. – Dev-iL Mar 21 '17 at 09:11
  • The action should be `com.android.launcher.action.INSTALL_SHORTCUT`. Creating a shortcut doesn't work for me either on the Samsung devices I have tested this on. – Jared Rummler Jun 16 '17 at 15:42
  • Update on my comment above. I was using `Intent.EXTRA_SHORTCUT_ICON` and using a `bitmap`. This caused a `TransactionTooLargeException`. I had to resize the bitmap. – Jared Rummler Jun 16 '17 at 22:32

1 Answers1

0

For adding shortcut first you have to add permission in Android:

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

and for above 23 API check for runtime permission for that use below link: Runtime Permission in Android

Now add shortcut:

private void createShortcut() {
    //Adding shortcut for SampleActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            SampleActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your App Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.mipmap.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);
}

If you find that multiple shortcuts created to avoid that you can check if the shortcut has been created or not:

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);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Harsh Mittal
  • 449
  • 3
  • 10