Android7.1之后的Shortcuts
介绍
相信大家都知道iOS的3DTouch吧,其实在苹果推出这种交互的时候,很多工程师就可以依靠软件程序实现类似交互了。Android7.1之后Google加入了这种交互方式,与iOS有一些不同,Google将其定义为一种快捷入口。
交互效果
这是目前我手中的一个项目(成长印记)已经实现了Android7.1的这种交互形式
我们看看官方的介绍吧
If your app targets Android 7.1 (API level 25) or higher, you can define shortcuts to specific actions in your app. These shortcuts can be displayed in a supported launcher. Shortcuts let your users quickly start common or recommended tasks within your app.
Each shortcut references one or more intents, each of which launches a specific action in your app when users select the shortcut. Examples of actions you can express as shortcuts include the following:
Navigating users to a particular location in a mapping app.
Sending messages to a friend in a communication app.
Playing the next episode of a TV show in a media app.
Loading the last save point in a gaming app.
You can publish two different types of shortcuts for your app:
Static shortcuts are defined in a resource file that is packaged into an APK. Therefore, you must wait until you update your entire app to change the details of these static shortcuts.
Dynamic shortcuts are published at runtime using the ShortcutManager API. During runtime, your app can publish, update, and remove its dynamic shortcuts.
App shortcuts on Nexus 6P
Figure 1: Using app shortcuts, you can surface key actions and take users deep into your app instantly.
You can publish up to five shortcuts (static shortcuts and dynamic shortcuts combined) at a time for your app. Some launcher apps, however, don't show every shortcut you've created for your app.
Users can copy your app's shortcuts onto the launcher, creating pinned shortcuts. There is no limit to the number of pinned shortcuts to your app that users can create. Your app cannot remove these pinned shortcuts, but it can disable them
理解之后
当你的Android版本在7.1及以上的时候那么你就可以定义有一些快捷方式以供用户更加迅速的定位到某个页面或功能,一个快捷方式支持一个或多个Intent的跳转
动态Shortcuts(DynamicShortcuts)
这种shortcuts事最为实应用的一种,其实我几乎就只用这一种,因为这样很好用,不想静态的,不易维护,固定不变的操作目前项目几乎没有了!
使用步骤
- 获取ShortcutManager
synchronized (ShortcutManager.class) {
if (manager == null) {
manager = context.getSystemService(ShortcutManager.class);
}
}
- 初始化shortcutInfoList
ShortcutInfo info = new ShortcutInfo.Builder(context, str)
.setShortLabel("")
.setLongLabel("")
.setIcon(Icon.createWithResource(context, R.drawable.ic_mine_prodution))
.setIntent(intent)
.setDisabledMessage(context.getString(R.string.shortcuts_disable_message))
.build();
shortcutInfoList.add(info);
- 设置getManager().setDynamicShortcuts
getManager().setDynamicShortcuts(shortcutInfoList);
删除Shortcuts
在这里说一下,Google没有所谓的删除Shortcuts,但是我们可以通过disableShortcuts方法进行取消快捷操作,但是实际测试中模拟器中并没有隐藏,而是我们设置的setDisabledMessage方法中的提示出现了!后来查了一下Shortcuts一旦被作为快捷方式独立显示在桌面那么程序是无法进行删除的,必须用户进行删除
静态(Static 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>
- 编辑资源文件
<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" />
<!-- If your shortcut is associated with multiple intents, include them
here. The last intent in the list determines what the user sees when
they launch this shortcut. -->
<categories android:name="android.shortcut.conversation" />
</shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>