Android 音樂播放器自己來 — Day 23 播放介面實作 (9) — Shortcut 基礎

fantasy1022
毅想世界
Published in
Feb 14, 2021

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

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:

3. 建立 res/xml/shortcuts.xml

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

  • 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介紹/

明天就來實作吧!

--

--