Android的App Shortcut功能

App Shortcut功能

最近手机在升级Android 7.1之后,长按某些APP图标就会弹出菜单,类似于IOS的3D touch功能,如下图:

这里写图片描述

看了系统更新的文档才知道该功能叫做App Shortcut,目前只有少部分的应用支持这个功能,之后随着Android版本的更新,将会有大批APP适配该功能。那我们就来看一下该功能是如何实现的:

实现App Shortcuts有两种形式:

  • 动态形式:在运行时,通过ShortcutManager API来进行注册。通过这种方式,你可以在运行时,动态的发布,更新和删除Shortcut。
  • 静态形式:在APK中包含一个资源文件来描述Shortcut。这种注册方法将导致:如果你要更新Shortcut,你必须更新整个应用程序
    目前,每个应用最多可以注册5个Shortcuts,无论是动态形式还是静态形式。

动态形式

通过动态形式注册的Shortcut,通常是特定的与用户使用上下文相关的一些动作。这些动作在用户的使用过程中,可能会发生变化。

ShortcutManager提供了API来动态管理Shortcut,包括:

  • 新建:方法setDynamicShortcuts() 可以添加或替换所有的shortcut;方法addDynamicShortcuts() 来添加新的shortcut到列表中,超过最大个数会报异常

  • 更新:方法updateShortcuts(List shortcutInfoList) 更新已有的动态快捷方式;

  • 删除:方法removeDynamicShortcuts(List shortcutIds) 根据动态快捷方式的ID,删除已有的动态快捷方式;方法removeAllDynamicShortcuts() 删除掉app中所有的动态快捷方式;

    下面是一段代码示例:

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));

静态形式

静态Shortcut应当提供应用程序中比较通用的一些动作,例如:发送短信,设置闹钟等等。

开发者通过下面的方式来设置静态Shortcuts:

App Shortcuts是在Launcher上显示在应用程序的入口上的,因此需要设置在action为“android.intent.action.MAIN”,category为“ android.intent.category.LAUNCHER”的Activity上。通过添加一个 <meta-data> 子元素来并指定定义Shortcuts资源文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">
  <application>
    <activity android:name="Main">
      <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>
</manifest>

在res/xml/shortcuts.xml这个资源文件中,添加一个 根元素,根元素中包含若干个 子元素,每个 描述了一个Shortcut,其中包含:icon,description labels以及启动应用的Intent。

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>

Shortcuts的简单作用

每个Shortcut可以关联一个或多个intents,每个intent启动一个指定的action; 官方给出了几个可以作为shortcut的例子,比如:

  • 在地图类app中,指导用户到特定的位置;
  • 在社交类app中,发送消息给一个朋友;
  • 在媒体类app中,播放视频的下一片段;
  • 在游戏类app中,下载最后保存的要点;

APP Shortcut功能很实用,能在自己的APP是适配该功能,用户体验将会进一步提高。

作者:何小冲
链接:Android的App Shortcut功能

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值