I'm trying to create a shortcut to an activity inside my application on the home screen. I managed to create the shortcut, but when i click on it nothing happens and it says "application not found" here's how my code looks like:

final Intent shortcutIntent = new Intent("com.myapp.action.MyActivity");
ComponentName name = new ComponentName(getPackageName(), ".MyActivity");
shortcutIntent.setComponent(name);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "blabla..");

Parcelable iconResource =
Intent.ShortcutIconResource.fromContext(PresetsActivity.this,R.drawable.ic_application_launcher);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

sendBroadcast(intent);
有帮助吗?

解决方案

None of the Intent constructors take a class name as an argument—you must pass the action instead. The usual action for launching an activity is ACTION_MAIN.

If you want to see some sample code for doing all kinds of fun things with shortcuts and intents, have a look at Shortcut Circus.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top