简体   繁体   中英

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.

在此处输入图片说明

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?

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

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.

The above snippet is a description for getDynamicShortcuts function in developer.android.com .

So, its better to use the API only for verification or to retrieve the details and not to set it back in ShortcutManager

For, further details, https://developer.android.com/reference/android/content/pm/ShortcutManager.html#getDynamicShortcuts()

Using ShortcutManager, we can add and remove dynamic app shortcuts in following way: The following method will create your app shortcut and will remove as well.

Eg.

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

}

And call this method in onCreate() method of your Launcher Activity. Note:- This is my sample code of dynamically adding and removing of app shortcuts menu. You can customise it as per your need. Here I am adding app shortcut only if user is logged into app and removing once user logged out.

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

Add this filter in your AndroidManifest.xml file under your Activity tag. Here my Activity name is "DonateActivity" where I navigate onClick of app shortcut menu.

<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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM