删除快捷方式副本

时间:2014-01-01 16:58:11

标签: android

我需要在安装应用程序时创建快捷方式并且代码正常工作 问题是,当我打开应用程序查看另一个快捷方式时 例如,如果我打开应用程序的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 );
 }

2 个答案:

答案 0 :(得分:1)

尝试使用此代码删除快捷方式:

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" />

在其他post

上查看我的答案

答案 1 :(得分:0)

答案非常简单,只需使用 intent.putExtra("duplicate", false); ,就不会创建重复项