Gatiko06
Gatiko06

Reputation: 299

Delete shortcut duplicate

I need create a shortcut when the app is installed and the code works fine the problem is that when I open the app view another shortcut example if I open 20 times the app this created 20 times shortcut

@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 );
 }

Upvotes: 0

Views: 498

Answers (2)

tanmeet
tanmeet

Reputation: 220

well the answer is very simple just use intent.putExtra("duplicate", false); and duplicates wont be created

Upvotes: 0

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

Try this code for deleting shortcuts:

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);

And make sure you are adding permission:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Check my answer on other post

Upvotes: 1

Related Questions