Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void createShortcut(Activity activity, Class activityToOpen, String title, @DrawableRes int icon) {
- Intent shortcutIntent = new Intent(activity, activityToOpen);
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
- Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
- intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
- intent.putExtra("duplicate", false);
- Parcelable parcelable = Intent.ShortcutIconResource.fromContext(activity, icon);
- intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, parcelable);
- activity.sendBroadcast(intent);
- } else {
- ShortcutManager shortcutManager = activity.getSystemService(ShortcutManager.class);
- if (shortcutManager != null && shortcutManager.isRequestPinShortcutSupported()) {
- ShortcutInfo pinShortcutInfo =
- new ShortcutInfo.Builder(activity, "browser-shortcut-")
- .setIntent(shortcutIntent)
- .setIcon(Icon.createWithResource(activity, icon))
- .setShortLabel(title)
- .build();
- shortcutManager.requestPinShortcut(pinShortcutInfo, null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement