Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 知識點小結(一):Intent 過濾器、meta-data 與 App Widget 配置

編輯:關於 Android 程式設計

清單檔(AndroidManifest.xml)裡有幾個節點初學時最容易寫錯,因為它們的名稱看起來樸素,實際職責卻差很多。本文挑出 intent-filter、meta-data 與 App Widget 的 receiver 逐項拆解。

一、一個 Activity 掛兩組 intent-filter

先看這段宣告:

<activity android:name=".LunchList"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />
    <meta-data android:name="android.app.default_searchable"
        android:value=".LunchList" />
</activity>

第一組過濾器宣告它是啟動圖示(MAIN + LAUNCHER),第二組則讓它能接收搜尋意圖。這裡的 <category android:name="android.intent.category.DEFAULT" /> 經常被誤認為可省略,但事實相反:凡是自訂的 Activity 想被隱式 Intent 啟動,就必須包含 DEFAULT 類別。因為系統在呼叫 startActivity() 時會自動為隱式 Intent 附加這個 category,缺了它就永遠匹配不上。

二、兩個 searchable meta-data 的差別

這兩行名字相似,作用卻完全不同:

android.app.searchable 搭配 @xml/searchable 資源,用來定義搜尋框的外觀與行為(提示文字、輸入 hint 等),name 是系統約定好的固定值,resource 指向你自己放在 res/xml/ 的設定檔。

android.app.default_searchable 搭配 .LunchList,指定由哪一個 Activity 負責執行搜尋並展示結果。前面那個 meta-data 只在掛載它的那個 Activity 內有效;加上這一個,等於全域宣告預設的搜尋處理者,任何頁面觸發搜尋都會導到 LunchList。這正是「想在任意 Activity 都能叫出搜尋」的關鍵。

三、App Widget 的 receiver 宣告

<receiver android:name=".AppWidget"
    android:label="@string/app_name"
    android:icon="@drawable/icon">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data
        android:name="android.appwidget.provider"
        android:resource="@xml/widget_provider" />
</receiver>

這裡 label 是 widget 在選單中顯示的名稱,icon 是它的圖示。intent-filter 過濾的是 widget 更新廣播,這同樣屬於隱式 Intent,因此 DEFAULT 類別照樣不能省。meta-data 中的 name 為固定值,resource 則指向我們自訂的參數設定檔。

四、widget_provider.xml 的參數

<appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
    android:minWidth="300dip"
    android:minHeight="79dip"
    android:updatePeriodMillis="1800000"
    android:initialLayout="@layout/widget"
/>

前三行分別是最小寬、最小高與更新間隔(單位為毫秒),第四行指向 widget 的實際佈局。關於 updatePeriodMillis 有一個重要現實:系統對 widget 更新頻率有下限限制,即使寫入更小的值也不會真正生效,短間隔更新應該改用 AlarmManager 或 WorkManager 自行排程。

五、widget 的佈局檔

寫法與 layout 目錄下的一般佈局相同:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/widget_frame"
>
    <TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/next"
        android:textSize="10pt"
        android:textColor="#FFFFFFFF"
    />

    <ImageButton android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ff"
    />
</RelativeLayout>

這裡有個實務限制值得記住:App Widget 的遠程佈局並非支援全部控制項,只接受 FrameLayout、LinearLayout、RelativeLayout、GridLayout 等少數容器,以及 TextView、ImageView、Button 等基本控制項。自訂 View 放進去不會顯示。另外,widget 內的點擊事件無法用一般的 setOnClickListener(),必須透過 PendingIntent 與 RemoteViews 綁定。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved