The code below creates a shortcut on the device's home screen, but by clicking this shortcut, it returns me an error "The app is not installed" How to solve? I would like this mainactivity to be opened with a specific parameter, to trigger an event when opened by this created shortcut.
Here is the code used:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
fabEmergencia.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
createShortCut();
return true;
}
});
private void createShortCut() {
Intent shortcutIntent = new Intent(activity,MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "YourAppName");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
activity.sendBroadcast(addIntent);
}