简体   繁体   English

Android动态快捷方式图标

[英]Android dynamic shortcuts icons

I have some problems with the new shortcuts from Android 7.1.1. Android 7.1.1的新快捷方式存在一些问题。

The second drawable has no resource id. 第二个可绘制对象没有资源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 现在,我找到了一种解决方法,但我希望他们能在下一个API更新中对其进行修复

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 getDynamicShortcuts

added in API level 25 List getDynamicShortcuts () Return all dynamic shortcuts from the caller app. 已在API级别25中添加。List getDynamicShortcuts()从调用者应用返回所有动态快捷方式。

This API is intended to be used for examining what shortcuts are currently published. 该API旨在用于检查当前发布了哪些快捷方式。 Re-publishing returned ShortcutInfos via APIs such as setDynamicShortcuts(List) may cause loss of information such as icons. 通过诸如setDynamicShortcuts(List)之类的API重新发布返回的ShortcutInfos可能会导致图标等信息丢失

The above snippet is a description for getDynamicShortcuts function in developer.android.com . 上面的片段是用于developer.android.com getDynamicShortcuts功能的描述。

So, its better to use the API only for verification or to retrieve the details and not to set it back in ShortcutManager 因此,最好只将API用于验证或检索详细信息,而不要在ShortcutManager中重新设置API

For, further details, https://developer.android.com/reference/android/content/pm/ShortcutManager.html#getDynamicShortcuts() 有关更多详细信息, 请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. 使用ShortcutManager,我们可以通过以下方式添加和删除动态应用程序快捷方式:以下方法将创建您的应用程序快捷方式,并且也会将其删除。

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. 并在启动器活动的onCreate()方法中调用此方法。 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. 将此过滤器添加到您的AndroidManifest.xml文件中“活动”标签下。 Here my Activity name is "DonateActivity" where I navigate onClick of app shortcut menu. 在这里,我的活动名称是“ 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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