android question
android question

Reputation: 23

how to automatically add application launcher icon to home screen when app is installed (android)

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" />

Upvotes: 2

Views: 4908

Answers (2)

Suresh Maidaragi
Suresh Maidaragi

Reputation: 2308

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

Upvotes: 4

Sandro Machado
Sandro Machado

Reputation: 10205

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

Upvotes: 1

Related Questions