Bob
Bob

Reputation: 23000

How can I pevent creating shortcut more than one time?

I have written the following code for creating a shortcut of my application in Home screen:

private void createShortcut() {

        Intent shortcutIntent = new Intent(this, ActActivation.class);
        shortcutIntent.setClassName("org.mabna.order",
                "org.mabna.order.ui.ActActivation");
        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, "test");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        this.sendBroadcast(addIntent);

    }

It works correctly but every time I run my program and this code is run, I get the message "Shortcut Test created" and a new shortcut is added to my home screen. After 10 time opening my application, I have 10 shortcut.

How can I prevent this message box and creating multiple shortcuts?

Upvotes: 1

Views: 4665

Answers (3)

Volodymyr Kulyk
Volodymyr Kulyk

Reputation: 6546

The best way to do this is to use SharedPreferences:

private static final String SHORTCUT = "SHORTCUT";
//...
if (!preferences.readBool(SHORTCUT, false)) {
    Intent shortcutIntent = new Intent(getApplicationContext(),
                    SomeActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                            R.drawable.app_icon));
    addIntent.putExtra("duplicate", false);

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

    SharedPreferences.Editor edit = preferences.edit();
    edit.putBoolean(SHORTCUT, true);
    edit.apply();
}

But, if user clear App Data and start App again, duplicate will be created.

Upvotes: 1

scottt
scottt

Reputation: 8371

Unfortunately, there's no API help in preventing duplicates. There's no API for seeing what's on the home screen either (at least not without hacking the launcher data). The best you can do is to track whether you've already created a short-cut or not. That's easiest to do with a shared preference. See the storage options guide.

It's better to go this way anyway. If you always creat a shortcut if it doesn't currently exist, you'll quickly alienate any user that has manually deleted your shortcut.

Upvotes: 1

Ronak Mehta
Ronak Mehta

Reputation: 5979

Create SharedPrefernce as well as add this Line , you dont need any other permission for Uninstallation

addIntent.putExtra("duplicate", false);

Code After Changes

    Intent shortcutIntent = new Intent(this, ActActivation.class);
    shortcutIntent.setClassName("org.mabna.order",
            "org.mabna.order.ui.ActActivation");
    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, "test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
    addIntent.putExtra("duplicate", false); // Just create once
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    this.sendBroadcast(addIntent);

This will create only single icon on HomeScreen , if user clear Cache this will prevent app from creating another Icon on Homescreen.

EDIT : Code is tested in android 4.2(JellyBean) where it is working fine.

Upvotes: 5

Related Questions