2

i created an android app. and what i want is when i install my app it should automatically add shortcut / launcher icon to home screen.

when i install my app a launcher icon should created automatically.

i tried this: but it's not working.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
  • possible duplicate of [How can I place app icon on launcher home screen?](http://stackoverflow.com/questions/4854197/how-can-i-place-app-icon-on-launcher-home-screen) – Matt K Nov 03 '14 at 17:33
  • To get a typical icon on the launcher screen, you should not normally need to do **anything** other than declare the usual intent filter for a launcher Activity in your manifest. ` ` – Chris Stratton Nov 03 '14 at 17:43

2 Answers2

4

Todo do this you need to do reverse Android because Home screen under user control, But still there is way Just create a method createShortCut() outside the oncreate method and call it inside onCreate(Bundle savedInstanceState) overridden method

private void createShortCut() {

     Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);
     shortcutIntent.setAction(Intent.ACTION_MAIN);
     Intent intent = new Intent();
     intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
     intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, R.string.app_name);
     intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
     intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
     getApplicationContext().sendBroadcast(intent);
}

Finally create boolean variable store it in shared preference before calling Above method makes sure boolean variable is false this is just to avoid multiple shortcuts. Don't forget to add permission in your Manifest<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> Hope it helps you

Suresh Maidaragi
  • 2,173
  • 18
  • 25
1

you need to send a broadcast:

//where this is a context (e.g. your current activity)
final Intent shortcutIntent = new Intent(this, SomeActivity.class);

final Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
// Sets the custom shortcut's title
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
// Set the custom shortcut icon
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon));
// add the shortcut
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);

More information here:Add Shortcut for android application To home screen On button click

Community
  • 1
  • 1
Sandro Machado
  • 9,921
  • 4
  • 36
  • 57