Android 7.1 的Shortcuts(快捷方式)

参考:https://blog.csdn.net/qibin0506/article/details/52878690

我这里就不解释什么是Shortcuts了,有什么不理解的看上面的地址,我这里只说如何实现,总结一下内容。

一、静态注册

第一步:在res/xml目录下创建一个新的xml文件, 这里我们命名为shortcuts.xml

<?xml version ="1.0" encoding ="utf-8"?>
https://developer.android.com/guide/actions/index.html -->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="home"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/short_name"
        android:shortcutLongLabel="@string/long_name"
        android:shortcutDisabledMessage="@string/no_name">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.myshortcuts"
            android:targetClass="com.example.myshortcuts.HomeActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>

首先一个shortcuts标签, 然后是一个shortcut, 到这里我们大概可以猜测到这里可以注册多个shortcut, shortcut标签有很多属性, 我们来一个个的了解下.

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

注意:shortcutShortLabel、shortcutLongLabel、shortcutDisabledMessage必须用string引入文本内容,不能直接输入文本。

 第二步:配置清单文件。必须在有<action android:name="android.intent.action.MAIN" />和<category android:name="android.intent.category.LAUNCHER" />的Activity中配置。

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <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>
    </application>

大功告成,静态配置就到这里。

二、动态配置

1.动态配置一个

    @SuppressLint("NewApi")
    private void initData() {
        mShortcutManager = getSystemService(ShortcutManager.class);
        List<ShortcutInfo> infos = new ArrayList<>();
        Intent intent = new Intent(this, MessageActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("msg", "哈哈哈");
        ShortcutInfo info = new ShortcutInfo.Builder(this, "id")
                .setShortLabel("哈")
                .setLongLabel("联系人:哈哈哈")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                .setIntent(intent)
                .build();
        infos.add(info);
        mShortcutManager.setDynamicShortcuts(infos);
    }

2. 动态配置多个(最多四个)

    @SuppressLint("NewApi")
    private void setupShortcuts() {
        for (int i = 0; i < 4; i++) {
            list.add("条目"+i);
        }
        mShortcutManager = getSystemService(ShortcutManager.class);
        List<ShortcutInfo> infos = new ArrayList<>();
        for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {//mShortcutManager.getMaxShortcutCountPerActivity()表示快捷方式的最大个数
            Intent intent = new Intent(this, MessageActivity.class);
            intent.setAction(Intent.ACTION_VIEW);
            intent.putExtra("msg", "我和" + list.get(i) + "的对话");
            ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
                    .setShortLabel(list.get(i))
                    .setLongLabel("联系人:" + list.get(i))
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                    .setIntent(intent)
                    .build();
            infos.add(info);
        }
        mShortcutManager.setDynamicShortcuts(infos);
    }

3. 删除和禁止Item

    private void removeItem() {
        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"));
    }

4. 修改了某个条目后,shortcut也应该相应的修改

    private void initData() {
        mShortcutManager = getSystemService(ShortcutManager.class);
        List<ShortcutInfo> infos = new ArrayList<>();
        Intent intent = new Intent(this, HomeActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra("msg", "嘿嘿嘿");
        ShortcutInfo info = new ShortcutInfo.Builder(this, "id1")
                .setShortLabel("涛")
                .setLongLabel("联系人:嘿嘿嘿!!!")
                .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher_round))
                .setIntent(intent)
                .build();
        infos.add(info);
        mShortcutManager.updateShortcuts(Arrays.asList(info));
    }

OK,就这样了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值