public void placeShortcut(Station station) { // credit: https://medium.com/@BladeCoder/using-support-library-26-0-0-you-can-do-bb75911e01e8 if (ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) { ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(mContext, station.getStationName()) .setShortLabel(station.getStationName()) .setLongLabel(station.getStationName()) .setIcon(IconCompat.createWithBitmap(createShortcutIcon(station))) .setIntent(createShortcutIntent(station)) .build(); ShortcutManagerCompat.requestPinShortcut(mContext, shortcut, null); Toast.makeText(mContext, mContext.getString(R.string.toastmessage_shortcut_created), Toast.LENGTH_LONG).show(); } else { Toast.makeText(mContext, mContext.getString(R.string.toastmessage_shortcut_not_created), Toast.LENGTH_LONG).show(); } }
/** * Create a shortcut via the AppCompat's shortcut manager. * <p> * On Android versions up to 7 shortcut will be created via system broadcast internally. * <p> * On Android 8+ the user will have the ability to add the shortcut manually * or let the system place it automatically. */ private static void installShortCutViaManager(Context context, Bitmap bitmap, String url, String title, boolean blockingEnabled) { if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) { final IconCompat icon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? IconCompat.createWithAdaptiveBitmap(bitmap) : IconCompat.createWithBitmap(bitmap); final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, UUID.randomUUID().toString()) .setShortLabel(title) .setLongLabel(title) .setIcon(icon) .setIntent(createShortcutIntent(context, url, blockingEnabled)) .build(); ShortcutManagerCompat.requestPinShortcut(context, shortcut, null); } }
private void createShortcut(){ Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); intent1.setAction(Intent.ACTION_VIEW); intent1.setData(Uri.parse(mSearchView.getText().toString())); intent1.putExtra("duplicate", false); ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(MainActivity.this, webViewTitle) .setShortLabel(webViewTitle) .setIcon(IconCompat.createWithBitmap(favoriteIcon)) .setIntent(intent1) .build(); ShortcutManagerCompat.requestPinShortcut(MainActivity.this, pinShortcutInfo, null); }
@Override public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) { EditText shortcutNameEditText = dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext); Intent intent1 = new Intent(getApplicationContext(), MainActivity.class); intent1.setAction(Intent.ACTION_VIEW); intent1.setData(Uri.parse(mSearchView.getText().toString())); intent1.putExtra("duplicate", false); ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(MainActivity.this, webViewTitle) .setShortLabel(shortcutNameEditText.getText().toString()) .setIcon(IconCompat.createWithBitmap(StaticUtils.getCircleBitmap(favoriteIcon))) .setIntent(intent1) .build(); ShortcutManagerCompat.requestPinShortcut(MainActivity.this, pinShortcutInfo, null); }
private boolean createShortcutWith(String id, String label, IconCompat icon) { ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(getContext(), id) .setIntent(intent) .setShortLabel(label) .setIcon(icon) .build(); return ShortcutManagerCompat.requestPinShortcut(getContext(), shortcut, startHomeScreen()); }
private void addShortcut(LayoutElementParcelable path) { //Adding shortcut for MainActivity //on Home screen final Context ctx = getContext(); if (!ShortcutManagerCompat.isRequestPinShortcutSupported(ctx)) { Toast.makeText(getActivity(), getString(R.string.addshortcut_not_supported_by_launcher), Toast.LENGTH_SHORT).show(); return; } Intent shortcutIntent = new Intent(ctx, MainActivity.class); shortcutIntent.putExtra("path", path.desc); shortcutIntent.setAction(Intent.ACTION_MAIN); shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // Using file path as shortcut id. ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(ctx, path.desc) .setActivity(getMainActivity().getComponentName()) .setIcon(IconCompat.createWithResource(ctx, R.mipmap.ic_launcher)) .setIntent(shortcutIntent) .setLongLabel(path.desc) .setShortLabel(new File(path.desc).getName()) .build(); ShortcutManagerCompat.requestPinShortcut(ctx, info, null); }