Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android provide us an intent class <code>com.android.launcher.action.INSTALL_SHORTCUT</code> which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut.</p> <p>First we need to add permission <code>INSTALL_SHORTCUT</code> to android manifest xml.</p> <pre><code>&lt;uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /&gt; </code></pre> <p>The <code>addShortcut()</code> method creates a new shortcut on Home screen.</p> <pre><code>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, "HelloWorldShortcut"); addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher)); addIntent .setAction("com.android.launcher.action.INSTALL_SHORTCUT"); addIntent.putExtra("duplicate", false); //may it's already there so don't duplicate getApplicationContext().sendBroadcast(addIntent); } </code></pre> <p>Note how we create shortcutIntent object which holds our target activity. This intent object is added into another intent as <code>EXTRA_SHORTCUT_INTENT</code>. </p> <p>Finally we broadcast the new intent. This adds a shortcut with name mentioned as <code>EXTRA_SHORTCUT_NAME</code> and icon defined by <code>EXTRA_SHORTCUT_ICON_RESOURCE</code>.</p> <p>Also put this code to avoid multiple shortcuts :</p> <pre><code> if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){ addShortcut(); getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload