Aerrow
Aerrow

Reputation: 12134

Issue while implementing shortcut for android application

I used below code for creating shortcut for my application, I added this method in my Splashscreen (which is my first screen) page, at first time of execution the short cut will created, now the problem is the shortcut is created at each time of my activity launch.

public boolean setShortCut(Context context, String appName) {
        System.out.println("in the shortcutapp on create method ");
        boolean flag = false;
        int app_id = -1;
        PackageManager p = context.getPackageManager();
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> res = p.queryIntentActivities(i, 0);
        System.out.println("the res size is: " + res.size());

        for (int k = 0; k < res.size(); k++) {
            System.out.println("the application name is: "
                    + res.get(k).activityInfo.loadLabel(p));
            if (res.get(k).activityInfo.loadLabel(p).toString().equals(appName)) {
                flag = true;
                app_id = k;
                break;
            }
        }

        if (flag) {
            ActivityInfo ai = res.get(app_id).activityInfo;

            Intent shortcutIntent = new Intent();
            shortcutIntent.setClassName(ai.packageName, ai.name);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Intent intent = new Intent();
            intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

            intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);

            intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(context,
                            R.drawable.app_icon));
            // intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            context.sendBroadcast(intent);
            System.out.println("in the shortcutapp on create method completed");
        } else
            System.out.println("appllicaton not found");
        return true;
    }

Upvotes: 2

Views: 161

Answers (1)

Nitin Sethi
Nitin Sethi

Reputation: 1336

Maintain a boolean in your app's shared preferences which is set to true once you create a shortcut.

if (flag && !isShortcutCreatedAlready()) {

    // Your code to create shortcut. Put the below code at the end of your try block

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("isShortcutCreated", true);
            editor.commit();
    }


    private boolean isShortcutCreatedAlready(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    return prefs.getBoolean("isShortcutCreated", false);
    }

Upvotes: 2

Related Questions