3

I have been trying to (ADD and then) REMOVE my APP's shortcut from HOME-SCREEN. ADDING a shortcut works perfectly however I'm not able to remove the shortcut I've created using below code.

public void setupShortCut(boolean create) {
        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);
        if(create) {
          intentShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        } else {
        intentShortcut.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
        }
        sendBroadcast(intentShortcut);
    }

Please suggest where am I going wrong?

EDIT 1:

I have required permission in my Manifest file:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /
Gagan
  • 1,497
  • 1
  • 12
  • 27

3 Answers3

4

Here you go:

private void deleteShortCut(Context context) {

    Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
    shortcutIntent.setClassName("com.example.androidapp", "SampleIntent");
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("someParameter", "HelloWorld");

    Intent removeIntent = new Intent();
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "ShortcutName");
    removeIntent.putExtra("duplicate", false);

    removeIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");       
    context.sendBroadcast(removeIntent);
}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
Devangi Desai
  • 1,355
  • 12
  • 17
  • Devangi, Thanks for your reply however your code is quite similar to what I have posted with just one exception, i.e.: `removeIntent.putExtra("duplicate", false);` I have added this code to my intent however it still doesn't work. – Gagan Oct 12 '12 at 06:50
  • Can you please try out this link http://codinggeekorg.wordpress.com/tag/home-screen-shortcuts/# – Devangi Desai Oct 12 '12 at 07:01
  • It was a 3rd party launcher which was restricting me to UNINSTALL the app shortcut I created. I have edited my question. – Gagan Oct 12 '12 at 09:03
  • Thanks for your code but this code doesn't work for above api level 21.so have you find any solution? – Ashish Mar 20 '17 at 12:27
1

For remove shortcut try with below code...

final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));          
intent.setComponent(new ComponentName(this.getPackageName(), "YourClassName"));                     
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");

sendBroadcast(intent, null);

add below permission to your Manifest file:

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />   
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • Thanks for the reply Priyank, however it still doesn't work. Is it working for you? – Gagan Oct 12 '12 at 07:05
  • Still can't get this to work. There is this thing with `SHORTCUTS` - in order to remove them using intent, you have to set intent's properties exactly the same way you've kept while creating them. – Gagan Oct 12 '12 at 07:29
  • It was a 3rd party launcher which was restricting me to UNINSTALL the app shortcut I created. I have edited my question. – Gagan Oct 12 '12 at 09:02
  • Is it possible to remove all shortcuts of an app, without telling the setComponent the class name of each of them ? Also, why do you need to add the name ? Is it really needed? – android developer Jul 25 '15 at 17:41
1

FIGURED OUT THE ROOT CAUSE :-)

I figured out the reason why it wasn't working for me. I am using a different 3rd party launcher (other than Stock android launcher) on my phone. CREATING and REMOVING an App-Shortcut works as long as the launcher you are using supports that operation. I ran the above code on default launcher and it works like a charm :)

Thank you everyone for your replies!

Gagan
  • 1,497
  • 1
  • 12
  • 27