简体   繁体   中英

Android shortcut not getting uninstalled

My app creates a shortcut on home screen when a particular button is pressed.But when I press the same button next time it is creating another shortcut without removing the previous shortcut. Is there any problem in my code

private void ShortcutIcon(){
              final String PREF_KEY_SHORTCUT_ADDED="Poo";

              // Checking if ShortCut was already added
            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean(PREF_KEY_SHORTCUT_ADDED, false);
    Toast.makeText(getApplicationContext(), "creating shortcut on desktop", 
               Toast.LENGTH_LONG).show();


  // if (shortCutWasAlreadyAdded) return;


    Intent shortcutIntent = new Intent(getApplicationContext(),              Shortcut.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, "SOS");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.sos));
    if (shortCutWasAlreadyAdded)
    {

        Intent shortcut = new Intent();
        shortcutIntent.setAction(Intent.ACTION_VIEW);


  shortcutIntent.setClassName(MainActivity.this, "com.example.urltesting.Shortcut");

   shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

   Intent removeIntent = new Intent();
   removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcut);
   removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SOS");
   removeIntent.putExtra("duplicate", false);

   removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
   Toast.makeText(getApplicationContext(), "trying to remove shortcut", 
           Toast.LENGTH_LONG).show();
   getApplicationContext().sendBroadcast(removeIntent);
    }


    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);

    // Remembering that ShortCut was already added
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(PREF_KEY_SHORTCUT_ADDED, true);
    editor.commit();
}

}

Did you add below permission in your manifest file?

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

Are you using a 3rd party launcher (other than Stock android launcher) on your phone? CREATING and REMOVING an App-Shortcut works as long as the launcher you are using supports that operation.

If you are using default android launcher then you can try out below code to remove/uninstall the app-shortcut :

    shortcutIntent = new Intent();
    shortcutIntent.setClassName("com.abc.xyz", "XYZActivity");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);

    Intent intentShortcut = new Intent();
    intentShortcut.putExtra("android.intent.extra.shortcut.INTENT", shortcutIntent);
    intentShortcut.putExtra("android.intent.extra.shortcut.NAME", getResources().getString(R.string.app_name));
    intentShortcut.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", icon);
    intentShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    sendBroadcast(intentShortcut);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM