天天看點

Android 應用快捷方式 Shortcuts(一)

Android 應用快捷方式 Shortcuts

簡介: 快捷方式可幫助使用者快速通路您的應用的某些部分,進而為他們呈現特定類型的内容。一次最多可以為應用釋出五個快捷方式(靜态和動态快捷方式加在一起),但大多數啟動器隻能顯示四個。不過,使用者可建立的應用固定快捷方式數量沒有限制。應用無法移除固定快捷方式,但仍然可以停用它們。

效果圖:

Android 應用快捷方式 Shortcuts(一)

1. 快捷方式的類型:

  • 靜态快捷方式:最适合在使用者與應用互動的整個生命周期内使用一緻結構連結到内容的應用。 由于大多數啟動器一次隻能顯示四個快捷方式,是以靜态快捷方式對常見 Activity 非常有用。例如,如果使用者希望以特定的方式檢視他們的月曆或電子郵件,使用靜态快捷方式可確定他們在執行日常任務時始終獲得一緻體驗。
  • 動态快捷方式:用于應用中與上下文相關的操作。 例如,如果您建構的遊戲允許使用者在啟動時就處于目前等級,則需要經常更新快捷方式。動态快捷方式允許遊戲在每次使用者通關後更新快捷方式。
  • 固定快捷方式:用于使用者驅動的特定操作。 例如,使用者可能需要将特定網站固定到啟動器。這很有用,因為它允許使用者執行自定義操作,比如一步導航到網站,這比使用浏覽器的預設執行個體速度更快。

1.2 建立靜态快捷方式

靜态快捷方式提供指向應用内正常操作的連結,這些操作在應用目前版本的生命周期内應保持一緻。适合使用靜态快捷方式的操作包括檢視已發郵件、設定鬧鐘以及顯示使用者當天的鍛煉活動。

如需建立靜态快捷方式,請按順序完成以下步驟:

  1. 在應用的清單檔案 (AndroidManifest.xml) 中,找到 intent 過濾器設定為 android.intent.action.MAIN 操作和 android.intent.category.LAUNCHER 類别的 Activity。
  2. 向此 Activity 添加

    <mate-data>

    元素,該元素引用了定義應用快捷方式的資源檔案:
<activity android:name=".MainActivity">
    <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>
           
  1. 建立新的資源檔案:res/xml/shortcuts.xml。

    在這個新的資源檔案中,添加

    <shortcuts>

    根元素,其中包含

    <shortcut>

    元素的清單。每個

    <shortcut>

    元素都包含有關一個靜态快捷方式的資訊,包括其圖示、說明标簽及其在應用内啟動的 intent:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="compose"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/short_label1"
        android:shortcutLongLabel="@string/long_label1"
        android:shortcutDisabledMessage="@string/disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.ecsu.shortcutsdemo"
            android:targetClass="com.ecsu.shortcutsdemo.MainActivity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

</shortcuts>
           

自定義屬性值:

必須為

android:shortcutId

android:shortcutShortLabel

提供值。其他所有的值均為可選。

android:shortcutId

:快捷方式的id,需要保證是唯一的。不能将此屬性的值設定為資源字元串(例如 @string/foo)。

android:shortcutShortLabel

:快捷方式的短名稱,長名稱顯示不下的情況下才會顯示短名稱,如果可能,将長度限制為10個字元。此屬性的值必須是資源字元串,例如 @string/shortcut_short_label。

android:shortcutLongLabel

:快捷方式的長名稱,如果有足夠的空間,Launcher會優先顯示長名稱,如果可能,将長度限制為25個字元。此屬性的值必須為資源字元串,例如 @string/shortcut_short_label。

android:icon

:快捷方式的圖示。

android:shortcutDisabledMessage

:當使用者嘗試啟動已禁用的快捷方式時提示的資訊,用于向使用者解釋為什麼禁用該快捷方式。如果

android:enabled="true"

,即快捷方式是啟用的,那麼此屬性的值無效。此屬性的值必須是資源字元串,例如@string/shortcut_disabled_message。

android:enabled

:是否啟用快捷方式,該值為true時,使用者點選快捷方式可以啟動指定的Intent操作;該值為false時,還需要指定

android:shortcutDisabledMessage

,當使用者點選快捷方式時會提示該資訊。禁用的快捷方式不會顯示出來,是以如果要禁用快捷方式,一般還是直接從xml檔案中移除比較好。

配置内部元素:

列出應用靜态快捷方式的 XML 檔案支援每個

<shortcut>

元素内的以下元素。必須為定義的每個靜态快捷方式添加一個 intent 内部元素。

android:action

:指定Intent要啟動的操作或任務,該屬性必須要指定,否則不會顯示快捷方式。

android:targetClass

:要跳轉的目标類。

android:targetPackage

:要跳轉的目标應用包名。

說明:

靜态快捷方式的配置雖然很簡單,但也是有數量限制的,最多隻能顯示4個,如果配置了超過4個,多餘的則不會顯示,應用并不會報錯。