編輯:進階開發
Android 的 Widget 用 Java 撰寫,比 W3C 那套 Widget 執行效率高得多,能做的事也多——你可以直接呼叫系統 API。除了 UI 上的天生限制(只能使用 RemoteViews 允許的元件),Widget 幾乎能當成一個輕量入口來用。這裡提供一份可直接套用的開發模板。
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(必要時再開一條執行緒)是標準解法。
<?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 名稱要用 $ 而非 . 分隔。
<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 或從服務端推播。
<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 的硬限制,設計時要先確認。
NDK 入門專案實戰(二)
hello-jni 為 apps 下 hello-jni 資料夾的名稱。這個指令會先找到 apps\hello-jni 下的 Application.mk,再找到 sou
多彩的 Android 手機系統:從 Dalvik 到應用市場的產業鏈效應
Dalvik 虛擬機器依賴 Linux 的一些特性,例如執行緒機制與底層記憶體管理;Linux 核心同時充當硬體與軟體堆疊之間的硬體抽象層。理解這一層關係,大致就掌握了
動手做一個 Android 貪食蛇:自訂 View 的繪製與刷新邏輯全解析
本文聚焦在 Android 的 UI 實作上,以經典的 Snake 貪食蛇專案為例,把自訂 View 的初始化、繪製與刷新機制講清楚。基本的操作這裡不多贅述,直接進主題。
Android 圖形系統剖析:SurfaceFlinger 與 Surface 的Client/Server架構
Android 的圖形系統採用 Client/Server 架構。核心的 SurfaceFlinger 主要由 C++ 撰寫,而 Client 端程式碼則分成兩部分:一部