12

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).

Cœur
  • 37,241
  • 25
  • 195
  • 267
user388269
  • 363
  • 2
  • 5
  • 10
  • I don't think the apis expose that functionality. You get an app icon in the pull up dialog by default. –  Jul 10 '10 at 04:28

3 Answers3

9

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 .

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • there might be another links that talk about it , such as this: http://code.google.com/p/apps-for-android/source/browse/#git%2FAnyCut and this: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/B9n6PjtTKic – android developer Sep 08 '12 at 08:36
5

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();
    } 
Virag Brahme
  • 2,062
  • 1
  • 20
  • 32
4

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" />
kord
  • 482
  • 3
  • 12