Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

App Widget 開發模板:從 Provider 到 Service

編輯:進階開發

Android 的 Widget 用 Java 撰寫,比 W3C 那套 Widget 執行效率高得多,能做的事也多——你可以直接呼叫系統 API。除了 UI 上的天生限制(只能使用 RemoteViews 允許的元件),Widget 幾乎能當成一個輕量入口來用。這裡提供一份可直接套用的開發模板。

Provider 與 Service 的分工

public class cwjWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                         int[] appWidgetIds) {
        // 把工作交給 Service,避免 ANR
        context.startService(new Intent(context, UpdateService.class));
    }

    public static class UpdateService extends Service {

        @Override
        public void onStart(Intent intent, int startId) {
            RemoteViews updateViews = buildUpdate(this);

            ComponentName thisWidget = new ComponentName(this, cwjWidget.class);
            AppWidgetManager manager = AppWidgetManager.getInstance(this);
            manager.updateAppWidget(thisWidget, updateViews);
        }

        public RemoteViews buildUpdate(Context context) {
            RemoteViews updateViews =
                    new RemoteViews(context.getPackageName(), R.layout.main);

            // 點擊 Widget 開啟系統資訊頁
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    context,
                    0,
                    new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS),
                    0);
            updateViews.setOnClickPendingIntent(R.id.ui, pendingIntent);

            // 取得系統版本資訊
            updateViews.setTextViewText(R.id.info,
                    android.os.Build.VERSION.CODENAME + " " + android.os.Build.ID);

            updateViews.setTextViewText(R.id.changelist,
                    android.os.Build.FINGERPRINT);

            return updateViews;
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;   // Service 不需要可 bindings
        }
    }
}

為什麼要繞道 Service?因為 onUpdate 是在主執行緒被呼叫的,一旦你在裡面做網路下載或大量運算,超過 5 秒就是 ANR。把實際工作丟給 Service(必要時再開一條執行緒)是標準解法。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.android123.widget"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk android:minSdkVersion="3" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".cwjWidget" android:label="android123_cwj">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/widget" />
        </receiver>

        <service android:name=".cwjWidget$UpdateService" />
    </application>
</manifest>

兩個細節:receiver 必須攔 APPWIDGET_UPDATE 這個 action,meta-data 則指向描述 Widget 基本資訊的 XML。內部長類的 service 名稱要用 $ 而非 . 分隔。

res/xml/widget.xml

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

updatePeriodMillis 有下限(約 30 分鐘),而且系統會把所有 Widget 的更新合併成批次送出,因此不要指望靠它做秒級刷新。真的需要即時性時,改用 AlarmManager 或從服務端推播。

res/layout/main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/ui"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip">

    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textSize="18dip" />

    <TextView
        android:id="@+id/changelist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginTop="4dip"
        android:textSize="9dip" />
</LinearLayout>

版面只能使用 RemoteViews 支援的元件(FrameLayout、LinearLayout、RelativeLayout、TextView、ImageView、ProgressBar、Button 等),這是 Widget UI 的硬限制,設計時要先確認。

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