15

I have an app that allows you to create "shortcuts" in Home Screen. But can i detect if the "shortcuts" already exist, i didn't have to create it again. Thanks.

user430926
  • 4,017
  • 13
  • 53
  • 77
  • 1
    The accepted answer still works on Lollipop, it just fails on Jelly Bean for an unknown reason to me. – ILikeTacos Apr 30 '15 at 01:10
  • 1
    @AlanChavez I had similar problem, adding action `Intent.ACTION_MAIN` to shortcut intent made solution work for me on all devices. – Seblis Aug 13 '15 at 14:36

3 Answers3

11

I was having the same issue. I don't think its possible for you to tell if it's there, but I think what I used will work for you as well.

Simply uninstall the shortcut before you add it!

intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(intent);

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

Just make sure you add the following to your manifest file.

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>    
Tyler
  • 19,113
  • 19
  • 94
  • 151
  • On a side note, the shortcut will re-appear where it was last placed. Very useful – Tyler Aug 10 '11 at 13:38
  • @Guy Uninstall will only work if the `SHORTCUT_INTENT` and the `SHORTCUT_NAME` that you put into the broadcast `Intent` actually match the shortcut that is on the user's homescreen. – David Wasser Nov 20 '12 at 13:59
  • If you want install the shortcut after the uninstall, you need to add the permission to your manifest file: – dayanruben Aug 06 '15 at 13:24
  • 3
    This does not work on late Android versions (>=Marshmallow) – Yehonatan Mar 07 '17 at 09:14
8

I do not know whether it is possible to detect the shortcut or not, but instead of uninstalling and then installing, you can use the Extra field as

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
...
intent.putExtra("duplicate", false);
...
sendBroadcast(intent);
Vasu
  • 4,862
  • 8
  • 42
  • 48
  • Have you try this? Can you explain in more detail about intent.putExtra("duplicate", false); Thanks. – user430926 Sep 19 '11 at 03:40
  • 1
    @user430926 If you set "duplicate" to `false` it will prevent duplicate shortcuts from being installed on the user's homescreen (ie: if this shortcut already exists on the homescreen it won't create another one). However, this does generate a toast saying 'Shortcut already exists' – David Wasser Nov 20 '12 at 14:01
  • 13
    Forget this method. No longer works as of Jellybean 2. It did work for older devices though - just tested it – SIr Codealot Mar 26 '13 at 16:15
  • 2
    @SIrCodealot this method still works -- It just doesn't work on any Jelly Bean version - just tested it on Lollipop – ILikeTacos Apr 30 '15 at 01:09
1

I think I found a good way to make sure the shortcut is not add more than once and also the Toast message to go away. Make a int or boolean (I used a int) and put the following in onResume:

if(shortcut != 1) //or shortcut == true
{
    ...(shortcut code)...
    shortcut = 1;
    savePref("shortcut", shortcut); //overriding method to save int's or booleans
}

If user delete's the user data then it will add another shortcut to the home screen but I can live with that since most users don't delete that kind of stuff anyway unless they are uninstalling the app. Hope this helps!

abotrix
  • 138
  • 1
  • 12
  • If the user manually added it himself or if the play store auto add it, he'd still end up with 2 shortcuts right? – SIr Codealot Mar 26 '13 at 16:07
  • The play store won't add a shortcut, its done by code. But if the user is tinkering with the apk, he or see must be an advance user. Installing the app your self will just do what the play store do unless you have it already on your home screen in which it would be already on your device. I believe if you update your app through the play store, I think it would uninstall the outdated version along with the old shortcut icon and re-install the new one. Once the user opens it, it should create an shortcut again... – abotrix Mar 26 '13 at 20:45
  • 2
    The play store won't add a shortcut, its done by code. > that's not true. There is an option in the latest store menu. – SIr Codealot Mar 28 '13 at 19:45
  • @mike is it only for latest version builds or can you please give me steps to add shortcut functionality on play store. thanks in advance. – Hradesh Kumar Apr 25 '14 at 07:11