8

Is there a way to determine whether a particular home screen shortcut exists?

My application installs a shortcut on the home screen at device startup time under certain conditions and I don't want duplicate shortcuts to appear. I also don't want Toast messages appearing saying "Shortcut created" or "Shortcut already exists" every single time the device boots. I've found an undocumented Intent Extra called EXTRA_SHORTCUT_DUPLICATE which will prevent duplicate shortcuts from being installed but the Launcher still displays the "Shortcut already exists" Toast message. I'd rather not rely on this undocumented Intent Extra if there is a supported technique for this.

asfman
  • 471
  • 1
  • 5
  • 9
  • 1
    +1 on this question, I asked a similar one: http://stackoverflow.com/questions/9452121/install-launcher-icon-in-home-screen-once . I'd really like a way to install the shortcut when the app is installed by the user, or when the user actively chooses from a popup provided by the app. – Wytze Feb 26 '12 at 10:09

3 Answers3

0

When your App create a shortcut,set "true" for a Boolean and store it in a storage(for example small file or sharedpreferences).And check it's value when your App try to create a shortcut.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
  • This will only tell you if your app created the shortcut or not. It won't tell you if the user has put the shortcut on the home screen himself, nor will it tell you if the user has removed the shortcut from the homescreen himself. – David Wasser Nov 19 '12 at 17:03
0

isn't that kind of intrusive? Why not just add it once, and let the user decide if they want to keep it or not?

edthethird
  • 6,263
  • 2
  • 24
  • 34
  • 1
    I agree, adding the shortcut just once is more than enough. I think Asfman has the same idea, and like me can only find code where the icon is installed in the onCreate event (i.e. every time the app is run). – Wytze Feb 26 '12 at 10:12
  • OP wants to be able to determine if it is already there! "Just adding it once" will still give you the "Shortcut already exists" toast if the user put it there himself **bofore** he starts the application. The most intelligent thing to do here is to first check if there already is a shortcut and if there isn't then you can automatically add one (or ask the user if he wants you to add one). And, no, it isn't intrusive. – David Wasser Nov 19 '12 at 17:02
  • newer versions of Android will automatically add shortcuts to the launcher when an application is installed. If I remove that shortcut, launch the app, and the shortcut is back this will require me to remove it again. If it adds a shortcut every single time I use the app (because I keep removing it) I will stop using the app. Let users control their launcher and homescreen, don't try to control it from your application. It is intrusive to attempt to modify other activity, and sandboxing prevents this for a very good reason. – edthethird Nov 19 '12 at 17:35
  • this is true,but if the user clears the application data,all the saving data is lost. Then when when starting ,the shortcut on the home screen will get twice.So it's better to know whether the shortcut existing or not. – RxRead Feb 21 '14 at 06:39
-1
**// Checking if ShortCut was already added
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        boolean shortCutWasAlreadyAdded = sharedPreferences.getBoolean("PREF_KEY_SHORTCUT_ADDED", false);
        if (shortCutWasAlreadyAdded) return;
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "SBM");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(addIntent);
        // Remembering that ShortCut was already added
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("PREF_KEY_SHORTCUT_ADDED", true);
        editor.commit();**