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资源文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.short1">
<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=".SettingsActivity"></activity>
<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>
</manifest>
在res/xml/shortcuts.xml这个资源文件中,添加一个 根元素,根元素中包含若干个 子元素,每个 描述了一个Shortcut,其中包含:icon,description labels以及启动应用的Intent。
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="settings"
android:enabled="true" //该二级菜单是否可用
android:icon="@drawable/ic_launcher_background" //显示的图标
android:shortcutShortLabel="@string/settings_short_name" //短名字
android:shortcutLongLabel="@string/settings_long_name" //长名字
android:shortcutDisabledMessage="@string/settings_disable_msg"> //不可用时显示的文字
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.short1"
android:targetClass="com.example.short1.SettingsActivity" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>
外部标签为<shortcuts>
,内部标签为<shortcut>
.如果有多个菜单的话,就写平级的<shortcut>
标签。
intent 的 targetPackage
要和manifest
的包名一致,targetClass
的值为目标页面值,<categories>
的标签内的 name 值是固定的。
Shortcuts的简单作用
每个Shortcut可以关联一个或多个intents,每个intent启动一个指定的action; 官方给出了几个可以作为shortcut的例子,比如:
- 在地图类app中,指导用户到特定的位置;
- 在社交类app中,发送消息给一个朋友;
- 在媒体类app中,播放视频的下一片段;
- 在游戏类app中,下载最后保存的要点;
APP Shortcut功能很实用,能在自己的APP是适配该功能,用户体验将会进一步提高。
参考博客:https://blog.csdn.net/he_xiaochong/article/details/73011584