Android create shortcuts on the home screen

前端 未结 10 875
忘掉有多难
忘掉有多难 2020-11-22 13:16

What I want to do is:

1) I\'m inside an activity, there are 2 buttons. If I click the first one a shortcut is created in my home screen. The shortcut open an

10条回答
  •  孤独总比滥情好
    2020-11-22 13:21

    I have developed one method below for creating the shortcut icon on android Homescreen [Tested on my own app]. Just call it.

    private void ShortcutIcon(){
    
        Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(addIntent);
    }
    

    Don't forget to change your activity name, icon resource & permission.

    
    

    Happy coding !!!

    Edit:

    For Duplicate Issue, First Option is to add below line in the code otherwise it creates new one every time.

    addIntent.putExtra("duplicate", false);
    

    Second Option is to First uninstall The App Shortcut Icon and then Install It Again If the First Option Didn't Work.

提交回复
热议问题