Create shortcuts in Home screen android

杀马特。学长 韩版系。学妹 提交于 2019-12-23 04:52:14

问题


This question may sound duplicate but there is no answer that i found is working:

I have gone through these question(s):

Android create shortcuts on the home screen

But the proposed solution is not working.

I have used the below solution which is working in API Level < 23

  Intent shortcutIntent = new Intent(context,
                LedgerDetailActivity.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, ledgerBean.getName());
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
        addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
        addIntent.putExtra("duplicate", false);
        context.sendBroadcast(addIntent);

Added Permission:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

But the above solution is not working in all devices and giving weird functionality sometimes like below:

  1. In some devices like Samsung Edge (with Android N) shortcuts are not getting created
  2. In my emulator with Android 7.1 (Android N) only one shortcut is getting created

Can someone please help me, There is no official documentation for this feature, Please don't confuse with App shortcuts introduced in Android N. I need shortcuts in home screen.


回答1:


For adding shortcut first you have to add permission in Android:

<uses-permission
    android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

and for above 23 API check for runtime permission for that use below link: Runtime Permission in Android

Now add shortcut:

private void createShortcut() {
    //Adding shortcut for SampleActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            SampleActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Your App Name");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.mipmap.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
    getApplicationContext().sendBroadcast(addIntent);
}

If you find that multiple shortcuts created to avoid that you can check if the shortcut has been created or not:

if(!getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).getBoolean(Utils.IS_ICON_CREATED, false)){
    addShortcut();
    getSharedPreferences(Utils.APP_PREFERENCE, Activity.MODE_PRIVATE).edit().putBoolean(Utils.IS_ICON_CREATED, true);
}


来源:https://stackoverflow.com/questions/42919589/create-shortcuts-in-home-screen-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!