0

while creating shortcut I get toast like "Shortcut created" and subsequently "Shortcut already exists".I have set the intent for duplicate and it doesnt create any duplicate. Now my doubt is

  1. How can i detect if the shortcut already exist.

  2. how can I disable the toast messages like "Shortcut created" and "Shortcut already exists".I have checked the LauncherModel(under com.android) api that shows the Toast,but app like WhatsApp dont show any Toast.

terminal
  • 31
  • 1
  • 3
  • possible duplicate of [How to determine if a home screen shortcut exists?](http://stackoverflow.com/questions/9322165/how-to-determine-if-a-home-screen-shortcut-exists) – David Wasser Nov 19 '12 at 17:08

3 Answers3

5

When creating shortcut you can add like this

addIntent.putExtra("duplicate", true);

(then when creating, create duplicate shortcut even it exist )

ex.

public void createShortcut(String url,,String classname,String shortcutName , String type)
    {           
        Intent shortcutIntent = new Intent();       
        shortcutIntent.setAction(Intent.ACTION_VIEW);

        if(type.equalsIgnoreCase("web")) {  

            Uri uri = Uri.parse(url);
            shortcutIntent.setData(uri);
        }
        else if (type.equalsIgnoreCase("app")) {


            shortcutIntent.setClassName(url,classname);
        }

        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, shortcutName);
        addIntent.putExtra("duplicate", true);

       addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, R.drawable.ic_launcher);

        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(addIntent);
//        Toast mToast = Toast.makeText(this, "shortcut created", Toast.LENGTH_SHORT);
//        mToast.show();

    }
UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
1

You must use uniqie ID for your shortcuts (For example shortcut name or id from server db)

In begging of your add shortcut method:

if (loadBoolean(context, String.valueOf(shortcutid))) return;

After sendbroadcast method you must call:

saveBoolean(context, true, String.valueOf(shortcutid));

Code of save and load methods:

 protected static boolean saveBoolean(Context context, boolean value, String tag) {
        try {
           PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(tag, value).apply();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    protected static boolean loadBoolean(Context context, String tag) {
        boolean value = false;
        try {
            value = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(tag, false);
        } catch (Exception ignored) {}
        return value;
    }
Peter Zverkov
  • 145
  • 1
  • 6
0

I also have the same problem like u, I've deleted the shortcut n wanted to create it again, but it keeps coming out with the message "Shortcut xxx already exist".

I tried changing the contact name and it works.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
Desris
  • 1