Create a shortcut code instance in Android

  • 2020-06-19 11:41:16
  • OfStack

1. Add permission (must)


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

2. Add shortcuts


    public static void setupShortcut(Activity activity)
    {
        Intent shortcutIntent = new Intent(activity, MainActivity.class); // Launch homepage ( launcher Activity)         Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "hello");// The name of the shortcut key can be arbitrary, but it is best app The name of the
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        intent.putExtra("duplicate", false);// Duplicate creation is not allowed         activity.sendBroadcast(intent);// Send broadcast create shortcut key
    }

3. The shortcut key can also point to non-ES9en activity, just add the following configuration to the corresponding Activity in AndroidManifest


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

For example, you can change MainActivity in 2 to any other Activity and add intent-ES20en to AndroidManifest.


Related articles: