Android7.0添加快捷方式(Shortcut)到手机桌面

长按应用图标弹出快捷方式,这个效果是Android 7.1之后新增的一个功能:应用快捷方式ShortcutManager,
官方API地址: https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html

桌面上长按应用图标弹出应用快捷列表,用户可以通过这些快捷键,直接访问应用具体的界面,并且长按这些快捷键可以在桌面上创建一个该快捷键的应用图标。

创建方式分为静态和动态的两种方式:
静态的: 在xml中定义, 适用于一些通用的动作.
动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.

注:该快捷方式Android 7.1,且每一个应用目前最多可以有5个shortcut。

一、静态创建:在资源文件下创建xml包,再生成一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!--shortcutShortLabel:拖动到桌面时显示的名字,官方建议不超过 10 个字符,必须字段-->
    <!--shortcutLongLabel:列表中每个shortcut的名字,不宜过长,如果过长或未设置默认会显示ShortLabel,官方建议不超过 25 个字符。可选字段。-->
    <!--shortcutDisabledMessage:为已固定在桌面的 shortcut 被 Disabled 后点击时的 Toast 提示内容。可选字段-->

    <shortcut
        android:enabled="false"
        android:icon="@drawable/ic_baby_head"
        android:shortcutId="set"
        android:shortcutLongLabel="@string/shortcut_long_set"
        android:shortcutShortLabel="@string/shortcut_short_set"
        android:shortcutDisabledMessage="@string/shortcut_long_set"
        tools:targetApi="n_mr1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.SettingActivity"
            android:targetPackage="com.shuniuyun.aiyoumi" />

    </shortcut>


    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_baby_head"
        android:shortcutId="info"
        android:shortcutLongLabel="@string/shortcut_long_info"
        android:shortcutShortLabel="@string/shortcut_short_info"
        android:shortcutDisabledMessage="@string/shortcut_msg_info"
        tools:targetApi="n_mr1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.PersonalInfoActivity"
            android:targetPackage="com.shuniuyun.aiyoumi" />

    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_baby_head"
        android:shortcutId="username"
        android:shortcutLongLabel="@string/shortcut_long_name"
        android:shortcutShortLabel="@string/shortcut_short_name"
        android:shortcutDisabledMessage="@string/shortcut_msg_name"
        tools:targetApi="n_mr1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.shuniuyun.aiyoumi.ui.mine.ChangeUsernameActivity"
            android:targetPackage="com.shuniuyun.aiyoumi" />

    </shortcut>
</shortcuts>

然后在清单文件中进行配置(注意在启动页面下进行配置)

        <activity android:name=".ui.main.StartActivity">
            <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>

二、动态创建:动态是通过 ShortcutManager API 进行操作,可以动态添加、修改、删除

  if (Build.VERSION.SDK_INT >= 25) {
            ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "shortcut_id_search")
                    .setShortLabelResId(R.string.lable_shortcut_static_search_disable)
                    .setLongLabelResId(R.string.lable_shortcut_static_search_disable)
                    .setIcon(Icon.createWithResource(this, R.drawable.ic_search))
                    .setIntent(new Intent(this, MainActivity.class))
                    .build();

            ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
            //这样就可以通过长按图标显示出快捷方式了
            shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));

        }

通过ShortcutInfo.Builder新建 ShortcutInfo,再通过shortcutManager添加即可。其中:

  • setDynamicShortcuts(List)可以替换并添加所有 shortcut 列表;
  • addDynamicShortcuts(List)可以添加新的 shortcut 到列表,超过最大个数会报异常;
  • updateShortcuts(List)可以更新一组 shortcuts;
  • removeDynamicShortcuts(List)和removeAllDynamicShortcuts() 可以删除部分或所有
    shortcuts。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值