user388269
user388269

Reputation: 363

How to add application shortcuts programmatically in Android

I need to add my Android application to home screen as shortcut programmatic.

Please give the idea for that. If possible please tell me how to manage existing shortcuts (deleting and adding some more shortcuts).

Upvotes: 12

Views: 15342

Answers (3)

Paresh Mayani
Paresh Mayani

Reputation: 128428

I have read an article which can help you in adding application Shortcut programmatically on Home Screen.

You can refer the example .

You can also refer the stackoverflow question related to shortcut here .

Upvotes: 9

kord
kord

Reputation: 479

I have spent quite a lot of time on trying different solutions from stackoverflow, but most of them are useless, because they are starting new instances of Activity. I need shortcut which works exactly like the one in app list or the one installed automatically by Google Play(start activity or bring already started Activity to front).

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //Save the flag to SharedPreferences to prevent duplicated shortcuts
        if (!settings.isShortcutAdded()) {
            addShortcut();
            settings.setShortcutAdded(true);
        }
    }

    private void addShortcut() {
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
        shortcutIntent.addFlags(flags);

        Intent addIntent = new Intent();
        addIntent.putExtra("duplicate", false);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                .fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }

And don't forget to update your manifest:

    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Upvotes: 4

Virag Brahme
Virag Brahme

Reputation: 2062

Call this method in your First screen's onCreate() method. Also ensure to check that app is running first time using SharedPreferences like I did :

 private void addShortcut() {
    //Adding shortcut for MainActivity on Home screen
    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, this.getResources().getString(R.string.app_name));
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                        R.drawable.ic_launcher));

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

    // TO check app is installed first time.
    SharedPreferences prefs = getSharedPreferences("ShortCutPrefs", MODE_PRIVATE);
    if(!prefs.getBoolean("isFirstTime", false)){
        addShortcut();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("isFirstTime", true);
        editor.commit();
    } 

Upvotes: 5

Related Questions