How to programmatically create a shortcut of another app in Android?

后端 未结 3 1253
说谎
说谎 2021-01-07 08:24

Suppose, I have some Android app which helps users to install some other apps. Is there any way to create a shortcut of this apps on home screen? Can I also specify the posi

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 08:47

    Try this :

    public void createShortCut() {
        Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutintent.putExtra("duplicate", false);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
        Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext, R.drawable.icon);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent("com.whatsapp"));
        sendBroadcast(shortcutintent);
    }
    

提交回复
热议问题