Android 动态快捷方式图标

Android dynamic shortcuts icons

我在使用 Android 7.1.1.

的新快捷方式时遇到了一些问题

第二个可绘制对象没有资源 ID。这是图片和代码片段。

private void createShortcuts(String deviceValue, String tablequery, int pos, String devImage, int index) {
    ShortcutManager shortcutManager = mActivity.getSystemService(ShortcutManager.class);

    if (index == 0) {

        List<ShortcutInfo> scInfo = shortcutManager.getDynamicShortcuts();

        Bundle b = new Bundle();
        b.putInt("position", pos);
        b.putString("table", tablequery);
        b.putString("device", devImage);

        String add = deviceValue + "_" + tablequery;
        ShortcutInfo shortcut = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

        scInfo.add(shortcut);

        shortcutManager.setDynamicShortcuts(scInfo);
    } else if (index == 1) {
        String remove = deviceValue + "_" + tablequery;
        shortcutManager.removeDynamicShortcuts(Arrays.asList(remove));
    }
}

我做错了什么?

现在我找到了解决方法,但我希望他们会在下一个 API 更新

中修复它

这里是截图,看起来不太好,但它有效:

private void createShortcuts(String deviceValue, String tablequery, int pos, String devImage, int index) {
    ShortcutManager shortcutManager = mActivity.getSystemService(ShortcutManager.class);
    List<ShortcutInfo> scInfo = shortcutManager.getDynamicShortcuts();

    if (index == 0) {

        Bundle b = new Bundle();
        b.putInt("position", pos);
        b.putString("table", tablequery);
        b.putString("device", devImage);

        String add = deviceValue + "_" + tablequery;

        if (scInfo.size() == 1) {
            ShortcutInfo webShortcut = null, webShortcut1 = null;

            webShortcut = new ShortcutInfo.Builder(mActivity, scInfo.get(0).getId())
                    .setShortLabel(scInfo.get(0).getShortLabel())
                    .setLongLabel(scInfo.get(0).getLongLabel())
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntent(scInfo.get(0).getIntent())
                    .build();

            webShortcut1 = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone_2))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut, webShortcut1));
        } else {
            ShortcutInfo webShortcut = new ShortcutInfo.Builder(mActivity, add)
                    .setShortLabel(deviceValue) // Shortcut Icon tab
                    .setLongLabel(deviceValue) // Displayed When Long Pressing On App Icon
                    .setIcon(Icon.createWithResource(mActivity, R.drawable.ic_shortcut_phone))
                    .setIntents(new Intent[]{
                            new Intent(Intent.ACTION_MAIN, Uri.EMPTY, mActivity, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                            new Intent(Intent.ACTION_DEFAULT, Uri.EMPTY, mActivity, Device_Detail_Activity.class).putExtras(b)
                    })
                    .build();

            shortcutManager.setDynamicShortcuts(Arrays.asList(webShortcut));
        }
    } else if (index == 1) {
        String remove = deviceValue + "_" + tablequery;
        shortcutManager.removeDynamicShortcuts(Arrays.asList(remove));
    }
}

getDynamicShortcuts

added in API level 25 List getDynamicShortcuts () Return all dynamic shortcuts from the caller app.

This API is intended to be used for examining what shortcuts are currently published. Re-publishing returned ShortcutInfos via APIs such as setDynamicShortcuts(List) may cause loss of information such as icons.

上面的代码片段是对 getDynamicShortcuts 函数 developer.android.com 的描述.

因此,最好仅将 API 用于验证或检索详细信息,而不要在 ShortcutManager 中重新设置

有关详细信息,https://developer.android.com/reference/android/content/pm/ShortcutManager.html#getDynamicShortcuts()

使用 ShortcutManager,我们可以通过以下方式添加和删除动态应用程序快捷方式: 以下方法将创建您的应用程序快捷方式,同时也会将其删除。

例如。

private void createShortCut(boolean createShortCut)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcut = new ShortcutInfo.Builder(MainActivity.this, "id1")
                .setShortLabel("Donate")
                .setLongLabel("Donate to make your contribution")
                .setIcon(Icon.createWithResource(MainActivity.this, R.drawable.ic_icon_new))
                .setIntents(
                        new Intent[]{
                                new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, DonateActivity.class)
                                        .putExtra("fromShortcut", true)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                        })
                .build();

        if (shortcutManager != null) {
            //create shortcuts only if user is logged in otherwise remove it
            if (createShortCut) shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
            else  shortcutManager.removeDynamicShortcuts(Collections.singletonList(shortcut.getId()));
        }
    }

}

并在启动器 Activity 的 onCreate() 方法中调用此方法。 注意:- 这是我动态添加和删除应用程序快捷方式菜单的示例代码。您可以根据需要自定义它。在这里,我仅在用户登录到应用程序时添加应用程序快捷方式,并在用户注销后删除。

          //check if user is logged in or not
        if (AppPreferences.contains("user_id"))
        {
             //user logged in -- create Shortcut
            createShortCut(true);
        }else
         {
            //user logged-out -- remove Shortcut
            createShortCut(false);
         }

将此过滤器添加到 AndroidManifest.xml 文件中的 Activity 标签下。在这里,我的 Activity 名称是 "DonateActivity",我在应用程序快捷菜单的 onClick 中导航。

<activity
        android:name=".DonateActivity"
        android:label="@string/Donate"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="com.mydonationapp.app.OPEN_DYNAMIC_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>