简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM