Shortcut for android for every version, android shortcut for oreo, android shortcut for nougat.

if(Build.VERSION.SDK_INT>=26){
            try{

                ShortcutManager shortcutManager=context.getSystemService(ShortcutManager.class);

                Intent shortcutIntent=new Intent(getApplicationContext(),ClassCalledOnShortcutClicked.class);
                shortcutIntent.setAction(Intent.ACTION_MAIN);

                ShortcutInfo postInfo=new ShortcutInfo.Builder(context,"ShortcutName")
                        .setShortLabel("ShortcutShortLabel")
                        .setLongLabel("ShortcutLongLabel")
                        .setDisabledMessage("DisableMessage")
                        .setIcon(Icon.createWithResource(context,R.mipmap.ic_launcher))
                        .setIntent(shortcutIntent)
                        .build();
                shortcutManager.setDynamicShortcuts(Arrays.asList(postInfo));

                //to pin shortcut to mobile in oreo      
           if (shortcutManager.isRequestPinShortcutSupported()) {
                    ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(context,"ShortcutName").build();
                    shortcutIntent= shortcutManager.createShortcutResultIntent(pinShortcutInfo);
                    PendingIntent successCallback = PendingIntent.getBroadcast(context, 0, shortcutIntent, 0);
                    shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());

                }
            }
            catch(Exception e){
                Log.e("shortcut error",e.getMessage().toString());
            }
        }
        else {
            try {

                Intent shortcutIntent = new Intent(getApplicationContext(),
                        EventDetailActivity.class);
                shortcutIntent.setAction(Intent.ACTION_MAIN);

                Intent addIntent = new Intent();
                addIntent
                        .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                                R.drawable.ic_launcher));

                addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate 
               getApplicationContext().sendBroadcast(addIntent);
           
                //default toast is not displayed in nougat so use toast to display the messsage
                if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N || Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1){
                    Toast.makeText(context,"Shortcut created",Toast.LENGTH_SHORT).show();

                }
            } catch (Exception d) {
            }
        }

Comments

Popular Post

Implement search functionality with help of edittext, rxjava and retrofit 2 in android.

QR code scanner using Zxing library. Scanner for android

Store GPS Location in image background android.

Storing captured image or an image picked from a gallery in internal storage using file provider android.