美文网首页
shortcuts 7.0以上Android系统长按弹出快捷方式

shortcuts 7.0以上Android系统长按弹出快捷方式

作者: newszhu | 来源:发表于2018-08-06 16:39 被阅读0次

一.静态添加

在res-xml 下新增 shortcuts.xml

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="settings"
        android:enabled="true"
        android:icon="@drawable/back"
        android:shortcutShortLabel="@string/settings_short_name"
        android:shortcutLongLabel="@string/settings_long_name"
        android:shortcutDisabledMessage="@string/settings_disable_msg">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.newheyd.JZKFcanjiren"
            android:targetClass="com.newheyd.JZKFcanjiren.Activity.AreaSelectActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts> 

参数解释:
1.shortcutId, 不用多说, 这肯定是一个唯一的id
2.enabled, 表示这个shortcut是否可用
3.shortcutShortLabel, 这里是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称
4.shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示
5.shortcutDisabledMessage, 这个配置是在我们选择一个不可用的shortcut时给用户的一个
6.intent, 这里表示我们点击shortcut时要干嘛,
1)targetPackage是指定一个目标应用的包名,
2)targetClass是我们要跳转的目标类, 这里要注意的是android:action一定要配置, 否则会崩溃
3)categories, 这个东西目前位置官方只给提供了android.shortcut.conversation

使用:

在带有

 <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

标识的的标签里添加如下代码:

<meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>

二.动态配置:(列表内容可变)

在带有标识的activity中添加以下方法:判断版本号25以上

 ShortcutManager mShortcutManager;
1.初始化,快捷方式列表
//   动态初始化快捷方式列表,, "id" + i  最好使用
唯一码进行标记,避免后续删除或者维护的时候出现问题
    @TargetApi(Build.VERSION_CODES.N_MR1)
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void setupShortcuts() {
        mShortcutManager = getSystemService(ShortcutManager.class);

        List<ShortcutInfo> infos = new ArrayList<>();
        for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {
            Intent intent = new Intent(this, PolicyQueryActivity.class);
            intent.setAction(Intent.ACTION_VIEW);
            intent.putExtra("msg", "我和" + "蕾蕾" + "的对话");

            ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
                    .setShortLabel("我和蕾蕾的对话" + i)
                    .setLongLabel("联系人:我和蕾蕾的对话" + i)
                    .setIcon(Icon.createWithResource(this, R.drawable.bottom_4_select))
                    .setIntent(intent)
                    .build();
            infos.add(info);
//            manager.addDynamicShortcuts(Arrays.asList(info));
        }

        mShortcutManager.setDynamicShortcuts(infos);
    }

2.将某一个失效的快捷方式禁用

//并不是移除快捷方式,只是设置为不可用。 快捷方式只有用户可以移除@id 快捷方式唯一码
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private void removeItem(String  id) {
        List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();
        for (ShortcutInfo info : infos) {
            if (info.getId().equals(id)) {
                mShortcutManager.disableShortcuts(Arrays.asList(info.getId()), "暂无该联系人");
            }
        }
        mShortcutManager.removeDynamicShortcuts(Arrays.asList(id));
    }

3.更新一条快捷方式的内容

//更新某一条快捷方式的信息
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private void updItem(int index) {
        Intent intent = new Intent(this, PolicyQueryActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("msg", "我和静静的对话");

        ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index)
                .setShortLabel("我和静静的对话-新")
                .setLongLabel("联系人:我和静静的对话-新")
                .setIcon(Icon.createWithResource(this, R.drawable.bottom_4_select))
                .setIntent(intent)
                .build();

        mShortcutManager.updateShortcuts(Arrays.asList(info));
    }

相关文章

网友评论

      本文标题:shortcuts 7.0以上Android系统长按弹出快捷方式

      本文链接:https://www.haomeiwen.com/subject/impwvftx.html