Android7.1之后的Shortcuts
介紹
相信大家都知道iOS的3DTouch吧翘瓮,其實(shí)在蘋果推出這種交互的時(shí)候桃犬,很多工程師就可以依靠軟件程序?qū)崿F(xiàn)類似交互了恨豁。Android7.1之后Google加入了這種交互方式沽瞭,與iOS有一些不同奠骄,Google將其定義為一種快捷入口豆同。
交互效果
這是目前我手中的一個(gè)項(xiàng)目(成長印記)已經(jīng)實(shí)現(xiàn)了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
理解之后
當(dāng)你的Android版本在7.1及以上的時(shí)候那么你就可以定義有一些快捷方式以供用戶更加迅速的定位到某個(gè)頁面或功能,一個(gè)快捷方式支持一個(gè)或多個(gè)Intent的跳轉(zhuǎn)
動(dòng)態(tài)Shortcuts(DynamicShortcuts)
這種shortcuts事最為實(shí)應(yīng)用的一種含鳞,其實(shí)我?guī)缀蹙椭挥眠@一種影锈,因?yàn)檫@樣很好用,不想靜態(tài)的,不易維護(hù)鸭廷,固定不變的操作目前項(xiàng)目幾乎沒有了枣抱!
使用步驟
- 獲取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);
- 設(shè)置getManager().setDynamicShortcuts
getManager().setDynamicShortcuts(shortcutInfoList);
刪除Shortcuts
在這里說一下,Google沒有所謂的刪除Shortcuts辆床,但是我們可以通過disableShortcuts方法進(jìn)行取消快捷操作佳晶,但是實(shí)際測試中模擬器中并沒有隱藏,而是我們設(shè)置的setDisabledMessage方法中的提示出現(xiàn)了讼载!后來查了一下Shortcuts一旦被作為快捷方式獨(dú)立顯示在桌面那么程序是無法進(jìn)行刪除的轿秧,必須用戶進(jìn)行刪除
靜態(tài)(Static Shortcuts)
- 清單文件設(shè)置
<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>