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
Post a Comment