0

How To Create Shortcut Automatically After Installation? Code Below Does't Work!

 private void createShortCut() {
     Intent intent = new Intent(ACTION_INSTALL_SHORTCUT);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
     intent.putExtra("duplicate", false); 
     Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//icon
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , MainActivity.class));

     setResult(RESULT_OK, intent);
     sendBroadcast(intent);
 }
thecr0w
  • 2,148
  • 4
  • 33
  • 59

2 Answers2

2

You cannot create shortcuts automatically after installation, because nothing of your code runs automatically after installation.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Yes it is very much possible to create shortcut be it your app or any third party app or system apps. Please follow this sample code.

  try
        {
            for (int i = 0; i < packageNameArray.length; i++)
            {
                Intent shortcutIntent = Util.makeIntent(MyPreference.getAction(context), MyPreference.getCategory(context), packageNameArray[i], ActivityNameArray[i]);
                Bitmap shortcutIcon2 = Util.confleteBitmap((UtilityClass.StringToBitMap(IconArray[i])), BitmapFactory.decodeResource(context.getResources(), R.drawable.shortcut_mark));
                data.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
                data.putExtra("android.intent.extra.shortcut.ICON", shortcutIcon2);
                data.putExtra("android.intent.extra.shortcut.NAME", AppNameArray[i]);
                //uninstalling shortcut to remove duplicacy
                Intent intent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
                intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                intent.putExtra("duplicate", false);
                intent.putExtras(data);
                context.sendBroadcast(intent);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

here packagenamearray is all apps you select to create shortcut you pass you app package name here if you have just your app shortcut to be created activitynamearray is activityname of selected app.

Flexo
  • 87,323
  • 22
  • 191
  • 272
tanmeet
  • 220
  • 2
  • 11