iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
0
Mobile Development

Android 音樂播放器自己來系列 第 23

播放介面實作(8) - Shortcut 基礎

除了 Widget 在桌面上可以直接控制 App 外,還可以透過 Shortcut 的功能,長按 App icon 後,就會跳出一個選單,Message 的 App 可以提供常用的聯絡人,使用者就可以直接點擊就可以直接進入相關的頁面。

https://ithelp.ithome.com.tw/upload/images/20200923/20129728VV6Lic2hNh.png
Ref: https://developer.android.com/guide/topics/ui/shortcuts

Shortcut 有分三種,在播放 App 可以提供一些相對應的選項:

  • Static shortcuts: 為固定選項,就由 App 提供,以播放器的例子可以提供播放功能。
  • Dynamic shortcuts: 為動態選項的,可以依照 App 執行的結果來決定,以播放器的例子以提供上次播放的最後一首歌曲。
  • Pinned shortcuts: 為使用者釘選的項目,使用者想要的客制選項,以播放器的例子可以提供使用者加入最愛的歌曲。

這邊先來實作 Static shortcuts,步驟為:

  1. 在 AndroidManifest.xml 內找到入口的 activity,就是點擊 App icon 後會開啟的第一個頁面,在 intent-filter 內有設定 android.intent.action.MAIN 和 android.intent.category.LAUNCHER。

  2. 加入對應的 shortcut 的

    <meta-data android:name="android.app.shortcuts"
                     android:resource="@xml/shortcuts" />
    
  3. 建立 res/xml/shortcuts.xml

  4. 設定 shortcuts 的 config,就類似前幾天(播放介面實作(6) - Widget 基礎)的 config

    <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>
    
    • android:icon:shortcuts圖示
    • android:shortcutLongLabel:長按時出現的label
    • android:shortcutShortLabel:將shortcuts拖曳出來後,固定在桌面上顯示的名稱
    • android:enabled:若為false,shortcut將無法使用,系統會將icon預設為灰階
    • android:shortcutDisabledMessage:點擊disable shortcuts toast顯示的錯誤訊息

    Ref: https://ironglion.com/archives/android-shortcuts介紹/

    明天就來實作吧!


上一篇
播放介面實作(7) - Widget 頁面(控制)
下一篇
播放介面實作(9) - ShortCut 頁面(功能)
系列文
Android 音樂播放器自己來30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言