繁体   English   中英

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

已在API级别25中添加。List getDynamicShortcuts()从调用者应用返回所有动态快捷方式。

该API旨在用于检查当前发布了哪些快捷方式。 通过诸如setDynamicShortcuts(List)之类的API重新发布返回的ShortcutInfos可能会导致图标等信息丢失

上面的片段是用于developer.android.com getDynamicShortcuts功能的描述。

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

有关更多详细信息, 请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()));
        }
    }

}

并在启动器活动的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文件中“活动”标签下。 在这里,我的活动名称是“ 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