Android7.1之后的Shortcuts

Android7.1之后的Shortcuts

介紹

相信大家都知道iOS的3DTouch吧,其實在蘋果推出這種交互的時候,很多工程師就可以依靠軟件程序實現類似交互了。Android7.1之后Google加入了這種交互方式,與iOS有一些不同,Google將其定義為一種快捷入口。

交互效果

屏幕快照 2017-05-05 21.27.34.png

這是目前我手中的一個項目(成長印記)已經實現了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>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容