Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. public static void createShortcut(Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
  2.         Intent shortcutIntent = new Intent(activity, activityToOpen);
  3.         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  4.             Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
  5.             intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  6.             intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
  7.             intent.putExtra("duplicate", false);
  8.             Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
  9.             intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
  10.             activity.sendBroadcast(intent);
  11.         } else {
  12.             ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
  13.             if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
  14.                 ShortcutInfo pinShortcutInfo =
  15.                         new ShortcutInfo.Builder(activity, "browser-shortcut-")
  16.                                 .setIntent(shortcutIntent)
  17.                                 .setIcon(Icon.createWithResource(activity, icon))
  18.                                 .setShortLabel(title)
  19.                                 .build();
  20.                 shortcutManager.requestPinShortcut(pinShortcutInfo, null);
  21.             }
  22.         }
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement