元數據不單單能傳遞簡單的字符串參數,還能傳送更復雜的資源數據,從Android7.1開始新增的快捷方式便用到了這點,譬如在手機上桌面長按支付寶圖標,會彈出如下圖所示的菜單。
點擊菜單項“掃一掃”,直接打開支付寶的掃碼頁面;點擊菜單項“付錢”,直接打開支付寶的付款頁面;點擊菜單項“收錢”,直接打開支付寶的收款頁面。如此不必打開支付寶首頁,即可迅速跳轉到常用的App頁面,這便是所謂的快捷方式。
那麼Android7.1又是如何實現快捷方式的呢?那得再琢磨琢磨元數據了。原來元數據的meta-data標籤除了前面說到的name屬性和value屬性,還擁有resource屬性,該屬性可指定一個XML文件,表示元數據想要的複雜信息保存於XML數據之中。藉助元數據以及指定的XML配置,方可完成快捷方式功能,具體的實現過程介紹如下:
首先打開res/values目錄下的strings.xml,在resources節點內部添加下述的三組(每組兩個,共六個)字符串配置,每組都代表一個菜單項,每組又分爲長名稱和短名稱,平時優先展示長名稱,當長名稱放不下時才展示短名稱。這三組字符串的配置定義示例如下:
<string name="first_short">first</string>
<string name="first_long">掃一掃</string>
<string name="second_short">second</string>
<string name="second_long">付錢</string>
<string name="third_short">third</string>
<string name="third_long">收錢</string>
接着在res目錄下創建名叫xml的文件夾,並在該文件夾創建shortcuts.xml,這個xml文件用來保存三組菜單項的快捷方式定義,文件內容如下所示:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="first"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/first_short"
android:shortcutLongLabel="@string/first_long">
<!-- targetClass指定了點擊該項菜單後要打開哪個活動頁面 -->
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.test"
android:targetClass="com.example.test.ScanActivity" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
<shortcut
android:shortcutId="second"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/second_short"
android:shortcutLongLabel="@string/second_long">
<!-- targetClass指定了點擊該項菜單後要打開哪個活動頁面 -->
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.test"
android:targetClass="com.example.test.PayActivity" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
<shortcut
android:shortcutId="third"
android:enabled="true"
android:icon="@mipmap/ic_launcher"
android:shortcutShortLabel="@string/third_short"
android:shortcutLongLabel="@string/third_long">
<!-- targetClass指定了點擊該項菜單後要打開哪個活動頁面 -->
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.test"
android:targetClass="com.example.test.ReceiActivity" />
<categories android:name="android.shortcut.conversation"/>
</shortcut>
</shortcuts>
由上面的xml例子看到,每個shortcut節點都代表了一個菜單項,該節點的各屬性說明如下:
shortcutId:快捷方式的編號。
enabled:是否啓用快捷方式。true表示啓用,false表示禁用。
icon:快捷菜單左側的圖標。
shortcutShortLabel:快捷菜單的短標籤。
shortcutLongLabel:快捷菜單的長標籤。優先展示長標籤的文本,長標籤放不下時才展示短標籤的文本。
以上的節點屬性僅僅指明瞭每項菜單的基本規格,點擊菜單項之後的跳轉動作還要由shortcut內部的intent節點定義,該節點主要有targetPackage與targetClass兩個屬性需要修改,其中targetPackage屬性固定爲當前App的包名,而targetClass屬性描述了菜單項跳轉頁面類的完整路徑。
然後打開AndroidManifest.xml,找到MainActivity所在的activity節點,在該節點內部補充如下的元數據配置,其中name屬性爲android.app.shortcuts,而resource屬性爲@xml/shortcuts:
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
這行元數據的作用,是告訴App首頁有個快捷方式菜單,其資源內容參見位於xml目錄下的shortcuts.xml。完整的activity節點配置示例如下:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 指定快捷方式。在桌面上長按應用圖標,就會彈出@xml/shortcuts所描述的快捷菜單 -->
<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
</activity>
然後把測試應用安裝到手機上,回到桌面長按應用圖標,此時圖標下方彈出下圖所示的快捷菜單。
點擊其中一個菜單項,果然跳到了配置的活動頁面,證明元數據成功實現了類似支付寶的快捷方式。