how to create a shortcut which leads to a non-launcher activity?

后端 未结 2 1551
轻奢々
轻奢々 2020-12-16 02:38

I want to create a shortcut in an android app, it lead to another activity which is not launcher of the app.

2条回答
  •  -上瘾入骨i
    2020-12-16 03:09

    I have developed one method below for creating the shortcut icon on android Homescreen. 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 . Happy coding !!!

提交回复
热议问题