>我需要在安装应用程序并且代码工作正常时创建一个快捷方式问题是当我打开应用程序查看另一个快捷方式时例如,如果我打开了 20 次应用程序这创建了 20 次快捷方式
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
borrarIcono();
agregarIcono();
}
private void agregarIcono(){
Intent shortcutIntents = new Intent(getApplicationContext(), Main.class);
shortcutIntents.addFlags(Intent.FLAG_ACTIVITY_NEW_ TASK);
shortcutIntents.addFlags(Intent.FLAG_ACTIVITY_CLEA R_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntents);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Whatsapp Imagenes");
addIntent.putExtra("DUPLICATE", false);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESO URCE, Intent.ShortcutIconResource.fromContext(getApplica tionContext(), R.drawable.icono));
addIntent.setAction("com.android.launcher.action.I NSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
private void borrarIcono(){
Intent shortcutIntent = new Intent(getApplicationContext(), Main.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_T ASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR _TOP);
shortcutIntent.putExtra("someParameter", "Whatsapp Imagenes");
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT , shortcutIntent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Whatsapp Imagenes");
removeIntent.setAction("com.android.launcher.actio n.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(removeIntent );
}
尝试以下代码删除快捷方式:
appShortcutIntent = new Intent();
appShortcutIntent.setClassName("com.pkg.pkgname", "MainActivity");
appShortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appIcon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intentDeleteShortcut = new Intent();
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.INTENT", appShortcutIntent);
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
intentDeleteShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", appIcon);
intentDeleteShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(intentDeleteShortcut);
并确保添加权限:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
在其他帖子上检查我的答案
答案很简单,只需使用intent.putExtra("duplicate", false);
,就不会创建重复项