App Shortcuts快捷方式

App Shortcuts简介

Android 7.1 - App ShortcutsAndroid 7.1 新功能之一就是 App Shortcuts(应用快捷方式) ,该功能与 iPhone 上的 3D Touch 功能相似,通过长按应用图标,可弹出应用快捷方式,点击可以直接跳转到相应的界面。目前最多支持 5 个快捷方式,可以 getMaxShortcutCountPerActivity() 查看 Launcher 最多支持几个快捷方式,不同的是 Android 支持通过拖拽将快捷方式固定到桌面。但是,实应用快捷方式还是有很多缺陷的:

  • 只能在 Google 的 Nexus 及 Pixel 设备上使用
  • 系统必须是 Android 7.1 及以上(API Level >= 25)
  • 已经被用户固定到桌面的快捷方式必须得到兼容性处理,因为你基本上失去了对其控制,除了升级时禁用

App Shortcuts注册方式

每一个应用目前最多可以有5个shortcuts(静态 + 动态),拥有静态注册/动态注册方式

运行条件:
应用添加App Shortcuts是Android 7.1(API 25)的API,所以只能在Android 7.1的设备上显示,同时需要launcher支持,比如Pixellauncher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)现在就支持,其他launcher也可以提供支持.

  • 静态的: 在xml中定义, 适用于一些通用的动作
  • 动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新

静态注册方式

静态的Shortcuts是写在xml中的, 直到下一次应用升级, 不能被改变.
添加静态shortcuts只需两步:
  1. 在应用的Manifest中启动Activity上添加<meta-data>

     <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>
    
  2. 在res/xml/目录下创建shortcuts.xml文件, 里面包含静态的shortcuts

     <?xml version="1.0" encoding="utf-8"?>
         <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
          <shortcut
              android:enabled="true"
              android:icon="@drawable/ic_check_circle_black_24dp"
              android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
              android:shortcutId="static"
             android:shortcutLongLabel="@string/static_shortcut_long_label_1"
              android:shortcutShortLabel="@string/static_shortcut_short_label_1">
     <intent
         android:action="android.intent.action.VIEW"
         android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
         android:targetPackage="com.ddmeng.hellonougat" />
     </shortcut>
     <shortcut
     android:enabled="true"
     android:icon="@drawable/ic_android_black_24dp"
     android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
     android:shortcutId="static_2"
     android:shortcutLongLabel="@string/static_shortcut_long_label_2"
     android:shortcutShortLabel="@string/static_shortcut_short_label_2">
     <intent
         android:action="android.intent.action.MAIN"
         android:targetClass="com.ddmeng.hellonougat.MainActivity"
         android:targetPackage="com.ddmeng.hellonougat" />
     <intent
         android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"
         android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
         android:targetPackage="com.ddmeng.hellonougat" />
      </shortcut>
     </shortcuts>
    

动态Shortcuts使用

动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.
使用ShortcutManager可以对动态shortcuts完成下面几种操作:

  • Publish发布: setDynamicShortcuts(), addDynamicShortcuts(List)
  • Update更新: updateShortcuts(List)
  • Remove删除: removeDynamicShortcuts(List), removeAllDynamicShortcuts()

比如添加一个动态shortcut:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("Web site")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
.setIntent(new Intent(Intent.ACTION_VIEW,
               Uri.parse("https://www.mysite.example.com/")))
.build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

文章参考:http://www.cnblogs.com/mengdd/p/5996665.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容