我想在主屏幕上创建快捷方式,我使用下面的代码:
Intent shortcut = new ntent("com.android.launcher.action.INSTALL_SHORTCUT");
ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Appname");
shortcut.putExtra("duplicate", false);
ComponentName component = new ComponentName(appInfo.packageName,"SplashScreenActivity.this");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(component));
Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getApplicationContext(), appInfo.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
getApplicationContext().sendBroadcast(shortcut);
这一个:
Intent shortcutIntent = new Intent(getApplicationContext(), 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, "Appname");
addIntent.putExtra("duplicate", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
当我点击快捷方式时,我得到了吐司应用程序不安装。 我已添加此权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
我尝试了很多其他解决方案,但总是遇到同样的问题。