Sebastian R. Sebastian R. - 29 days ago 14
Android Question

Android dynamic shortcuts icons

I have some problems with the new shortcuts from Android 7.1.1.

The second drawable has no resource id. Here is and image and the code snippet.

enter image description here

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));
}
}


What am I doing wrong?

Answer

Now i found a workaround but i hope they will fixed it in the next API Update

Here is the snipped looks not really nice but it work:

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));
    }
}