我已经在主屏幕上创建了应用程序快捷方式。但它不起作用。它总是显示吐司app isn't installed。我尝试了2种不同的代码,但结果始终相同。
这是第一个代码
private void addShortcut() {
// Adding shortcut on Home screen
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "AppName");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
getApplicationContext(), R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
Run Code Online (Sandbox Code Playgroud)
第二码
public static void addShortcut(Context context)
{
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = context.getApplicationInfo();
// Shortcut name
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "appInfo.name");
shortcut.putExtra("duplicate", false); // Just create once
// Setup activity shoud be shortcut object
ComponentName component = new ComponentName(appInfo.packageName, "MainActivity.class");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, …Run Code Online (Sandbox Code Playgroud)