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

Android Widget 開發完整流程:以叽歪小工具為例

編輯:關於 Android 程式設計

桌面小工具(App Widget)是 Android 相當有特色的一塊,它跟一般 Activity 完全不同:不能自由使用元件,只能透過 RemoteViews 更新畫面。本文以一個「輸入帳號後從叽歪(Plurk)抓取內容並顯示在桌面」的小工具為例,把整個開發步驟走一遍。

測試帳號:使用者 students、密碼 111111(僅供測試,請勿修改)。

步驟一:建立 AppWidgetProvider

新增一個類別並繼承 AppWidgetProvider。這個類別提供了 onDelete()、onEnable()、onDisabled() 等回呼,但實務上絕大多數情況只需要覆寫 onUpdate()。

在 onUpdate() 裡,通常會啟動背景執行緒或 Service 去抓資料,再把結果寫回 Widget。原因是 onUpdate() 跑在主執行緒上,網路請求不能直接在裡面做。

步驟二:在 AndroidManifest.xml 註冊 receiver

<receiver android:name="JiwaiWidget"
    android:label="@string/app_name"
    android:icon="@drawable/jiwai">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/info" />
</receiver>

逐行說明:

  • android:name:指向你的 AppWidgetProvider 子類,本例為 JiwaiWidget。
  • android:label:小工具在選單上顯示的名稱,取自 values/string.xml 的 app_name。
  • android:icon:小工具圖示,取自 drawable 下的 jiwai 圖片。
  • intent-filter:必須接收 APPWIDGET_UPDATE 廣播,否則永遠不會收到更新通知。
  • meta-data:指向描述檔,定義尺寸、更新間隔等資訊。

步驟三:撰寫 provider 描述檔 res/xml/info.xml

<appwidget-provider xmlns:android="https://schemas.android.com/apk/res/android"
    android:minWidth="200dp"
    android:minHeight="90dp"
    android:updatePeriodMillis="43200000"
    android:initialLayout="@layout/appwidget"
    android:configure="com.lawrenst.jiwai.JiwaiConfigure">
</appwidget-provider>

各屬性含義:

  • minWidth / minHeight:小工具的最小尺寸,決定它能被放到多小的格子。
  • updatePeriodMillis:系統自動更新的間隔(毫秒)。要注意系統對這個值有下限限制,寫太小不會真的那麼頻繁。
  • initialLayout:小工具的版面配置檔。
  • configure:可選。若小工具在加入桌面時需要先跳出 Activity 讓使用者設定,就指向該 Activity。本例因為要先輸入帳號密碼,所以必須設定。

步驟四:小工具版面 res/layout/appwidget.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/widget"
    android:background="@drawable/title_a">

    <LinearLayout android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:background="@drawable/title">
        <TextView android:id="@+id/username_display"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textColor="#ffffff"
            android:textSize="15px"
            android:gravity="left|center_vertical"
            android:paddingLeft="6px" />
    </LinearLayout>

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:textColor="#ffffff"
            android:textSize="12px"
            android:gravity="center_vertical|left"
            android:paddingLeft="6px"
            android:layout_height="30px">
        </TextView>
        <TextView android:id="@+id/text2"
            android:textColor="#ffffff"
            android:layout_height="30px"
            android:gravity="center_vertical|left"
            android:textSize="12px"
            android:paddingLeft="6px"
            android:layout_width="fill_parent">
        </TextView>
    </LinearLayout>
</LinearLayout>

共三個 TextView:一個顯示使用者名稱,兩個顯示抓回來的叽歪內容。

這裡有個重要限制要牢記:Widget 版面不支援所有控制項,可用的是 FrameLayout、LinearLayout、RelativeLayout、GridLayout 這幾種 Layout,加上 AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper 等少數元件。像 ListView 在早期版本就無法直接使用,自訂 View 也不行。

步驟五:設定用的 Activity(res/layout/login.xml)

因為需要讓使用者輸入帳號密碼,必須額外做一個登入畫面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textColor="#ff8c00"
        android:capitalize="characters"
        android:textStyle="bold" />

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/user"
            android:textColor="#ff8cff"
            android:capitalize="characters" />
        <EditText android:id="@+id/username"
            android:layout_width="200px"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/code"
            android:textColor="#ff8cff"
            android:capitalize="characters" />
        <EditText android:id="@+id/password"
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:password="true" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <Button
            android:id="@+id/submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit" />
    </LinearLayout>
</LinearLayout>

兩個 EditText 分別接收帳號與密碼(密碼欄用 android:password="true" 遮罩輸入內容),加上一個送出用的 Button。

完整開發順序回顧

  1. 繼承 AppWidgetProvider,覆寫 onUpdate(),在裡面啟動背景作業抓資料。
  2. 在 Manifest 註冊 receiver,加上 APPWIDGET_UPDATE 的 intent-filter 與 meta-data。
  3. 撰寫 res/xml/info.xml 描述尺寸、更新間隔與初始版面。
  4. 撰寫 Widget 版面配置,只用平台支援的 Layout 與控制項。
  5. 若需初始設定,多做一個 Activity 並在 configure 屬性中指向它。

另外提醒:由於 Widget 的更新是透過 RemoteViews 操作,抓回來的資料要記得存到能跨程序讀寫的地方(例如 SharedPreferences 或 ContentProvider),並在 onUpdate() 裡用 AppWidgetManager.updateAppWidget() 推送到桌面。

本文原始作者:wenhuihhwh

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