Android7.0新特性Shortcut

转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/68962736

Shortcut概念

Shortcut 是Android-25(Android 7.1)新增的一项类似iOS的 3D Touch 功能的快捷方式组件,但是有着不同的表现形式,因为Android在硬件上不支持触摸压力感应,所以表现形式为长按,而iOS须用力长按。

首先,来个效果图

     

在 Launcher 或 应用程序列表 里面,长按应用图标,弹出一个快捷方式列表, 并且,可以把单个快捷方式拖动出来作为一个桌面图标,拖出来的图标会随着清除应用数据或卸载应用而消失,须重新创建。

具体实现

BuildConfig 配置

在主module下,修改 build.grade,使其使用 android-25 的 API 编译,当然,未下载的,就需要打开Android SDK Manager下载一下。

android {
    compileSdkVersion 25 
    buildToolsVersion "25.0.0" // 或以上

    defaultConfig {
        targetSdkVersion 25
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

静态配置

类似BroadCastReceiver,Shortcut注册也分为静态注册和动态注册,首先介绍静态注册,动态注册后面继续~~

  1. 在 res/xml 文件夹底下创建一个xml,举个栗子:shortcut.xml

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_bar_detail_write"
        android:shortcutDisabledMessage="@string/shortcut_publish"
        android:shortcutId="publish"
        android:shortcutLongLabel="@string/shortcut_publish"
        android:shortcutShortLabel="@string/shortcut_publish">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
            android:targetPackage="com.yanshi.writing" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/logo"
        android:shortcutDisabledMessage="@string/shortcut_write"
        android:shortcutId="write"
        android:shortcutLongLabel="@string/shortcut_write"
        android:shortcutShortLabel="@string/shortcut_write">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.yanshi.writing.ui.write.WriteActivity"
            android:targetPackage="com.yanshi.writing" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    </shortcuts>
       
       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    1、enabled:表示当前快捷方式是否可使用 
    2、 icon: 快捷方式图标 
    3、 shortcutDisabledMessage: 快捷方式不可使用时显示的名字 
    4、 shortcutId:快捷方式标识 
    5、 shortcutLongLabel:长按下图标弹出来列表框中每个快捷名 
    6、 shortcutShortLabel:快捷是可以单独显示在桌面上的,显示名为shortcutShortLabel 
    7、 targetClass:点击快捷方式进入的Activity 
    8、categories 默认写死即可

  2. 清单文件注册 
    在 AndroidMainfest.xml 的默认启动页里添加 meta-data 标签配置

    <activity
            android:name=".ui.MainActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoneTranslucent">
            <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/shortcut" />
        </activity>
       
       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  3. 完毕! 可以到桌面查看效果了~~

动态配置

动态创建增加了菜单配置的灵活性,比如可以从服务端拉取快捷方式列表,再进行展示。具体配置方法如下:

创建

在需要注册的地方添加如下代码:

/**
  * 动态创建
  */
 public void register() {
     ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
     List<ShortcutInfo> infos = new ArrayList<>();

     // 按下返回按钮跳转的activity
     Intent intent1 = new Intent(this, MainActivity.class);
     intent1.setAction(Intent.ACTION_VIEW);

     // 目标activity
     Intent intent2 = new Intent(this, PublishPostActivity.class);
     intent2.setAction("com.yuyh.xxx.BACK");

     Intent[] intents = new Intent[2];
     intents[0] = intent1;
     intents[1] = intent2;

     ShortcutInfo info = new ShortcutInfo.Builder(this, "publish-2")
             .setShortLabel("动态创建-发布帖子")
             .setLongLabel("动态创建-发布帖子")
             .setIcon(Icon.createWithResource(this, R.mipmap.ic_bar_detail_write))
             .setIntents(intents)
             .build();
     infos.add(info);

     mShortcutManager.setDynamicShortcuts(infos);
 }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

重新运行app,再次长按,效果如下:

删除或禁用

动态删除可以删除动态配置的快捷方式。

/**
 * 动态删除
 */
public void delete() {

    ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);

    /********* 移除弹出列表图标 **********/
    // 所有动态创建图标
    List<ShortcutInfo> infos1 = mShortcutManager.getDynamicShortcuts();

    List<String> ids1 = new ArrayList<>();
    for (ShortcutInfo info : infos1 ) {
        ids1.add(info.getId());
    }

    // 禁用所有的快捷方式
    mShortcutManager.disableShortcuts(ids1, "已禁用");
    mShortcutManager.removeDynamicShortcuts(ids1);

    /********* 移除拖出来的桌面快捷图标 **********/
    // 放在桌面的图标
    List<ShortcutInfo> infos2 = mShortcutManager.getPinnedShortcuts();

    List<String> ids2 = new ArrayList<>();
    for (ShortcutInfo info : infos2 ) {
        ids2.add(info.getId());
    }

    mShortcutManager.disableShortcuts(ids2, "已禁用");
    mShortcutManager.removeAllDynamicShortcuts();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

代码比较简单,就不多做叙述了。 须注意一下 getPinnedShortcuts 方法与 getDynamicShortcuts 方法的区别! 禁用后的效果如图所示,图标变成灰色:

更新

快捷方式的唯一性,由前面提到的 shortcutId 这个标识符决定,所以更新快捷方式与创建快捷方式一样, shortcutId 如果相同, 则会覆盖之前创建的快捷方式!

返回栈问题

当通过快捷方式打开时,现有的Activity都会被销毁,然后重新创建一个Activity栈。因为清单方式设置的快捷键的Intent不能自定义Intent的Flag,其默认的Flag是 FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK

通过动态注册的方式,可发现,我们可以配置返回目标activity。当然,静态配置也可以实现,修改shortcut标签:

<shortcut
     android:enabled="true"
     android:icon="@mipmap/ic_bar_detail_write"
     android:shortcutDisabledMessage="@string/shortcut_publish"
     android:shortcutId="publish"
     android:shortcutLongLabel="@string/shortcut_publish"
     android:shortcutShortLabel="@string/shortcut_publish">

     <!-- 返回目标activity -->
     <intent
         android:action="com.yuyh.xxx.BACK"
         android:targetClass="com.yanshi.writing.ui.MainActivity"
         android:targetPackage="com.yanshi.writing" />

     <!-- 目标activity -->
     <intent
         android:action="android.intent.action.VIEW"
         android:targetClass="com.yanshi.writing.ui.bar.PublishPostActivity"
         android:targetPackage="com.yanshi.writing" />
     <categories android:name="android.shortcut.conversation" />
 </shortcut>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

感谢阅读!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值