編輯:關於 Android 程式設計
Android 裡各種元件大多屬於 Widget 類別,但我們平常講的 Widget 通常指 AppWidget,也就是可以放到桌面上的小部件。下面用兩個實例說明它的用法。
<?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="fill_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="今晚打老虎" />
</LinearLayout>
這個檔案用來描述部件的中繼資料,例如更新頻率、最小尺寸、初始佈局等。
package com.example.d_appwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
public class AppWidget extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
// 首次加入部件
super.onEnabled(context);
System.out.println("--> onEnabled");
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// 更新部件
super.onUpdate(context, appWidgetManager, appWidgetIds);
System.out.println("--> onUpdate");
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// 刪除部件
super.onDeleted(context, appWidgetIds);
System.out.println("--> onDeleted");
}
@Override
public void onDisabled(Context context) {
// 刪除最後一個部件
super.onDisabled(context);
System.out.println("--> onDisabled");
}
@Override
public void onReceive(Context context, Intent intent) {
// 接收廣播,是相當關鍵的方法
super.onReceive(context, intent);
System.out.println("--> onReceive");
}
}
<receiver android:name="AppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidgetprovider_info" />
</receiver>
運行後,在桌面的「小部件」清單中就能看到我們自訂的部件。

執行新增、刪除等動作時,我理解的方式是:系統發出不同的廣播,走到對應的 callback 方法,處理完後再發出特定廣播。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="電筒-開" />
</LinearLayout>
一樣描述部件的中繼資料。
重點在於:AppWidget 的佈局是 RemoteViews,不能直接 findViewById 後操作,必須透過 PendingIntent 綁定點擊事件,再在 onReceive 中接收自訂廣播並更新介面。
package com.example.e_appwidget_flashlight;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.widget.RemoteViews;
public class AppWidget extends AppWidgetProvider {
private static final String ACTION_TAG = "leelit.action.flash"; // 自訂廣播
private static boolean flashOn = false;
private static Camera camera;
private static Camera.Parameters parameters;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(ACTION_TAG);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.appwidget_layout);
// 把點擊事件綁到按鈕上
remoteViews.setOnClickPendingIntent(R.id.button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_TAG)) {
// 收到點擊廣播後,重新取得 RemoteViews 才能改動介面
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.appwidget_layout);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName provider = new ComponentName(context, AppWidget.class);
flashOn = !flashOn;
if (flashOn) {
turnOnLight();
remoteViews.setTextViewText(R.id.button, "電筒-關");
appWidgetManager.updateAppWidget(provider, remoteViews);
} else {
turnOffLight();
remoteViews.setTextViewText(R.id.button, "電筒-開");
appWidgetManager.updateAppWidget(provider, remoteViews);
}
}
}
private void turnOnLight() {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.release(); // 必須釋放,否則後續關不掉
}
private void turnOffLight() {
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.release();
}
}
<receiver android:name="com.example.e_appwidget_flashlight.AppWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<!-- 自訂廣播 -->
<action android:name="leelit.action.flash" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidgetprovider_info" />
</receiver>
別忘了加上 CAMERA/FLASHLIGHT 相關權限。完成後,在桌面加入部件,點一下就能開關手電筒。
更多細節可參考附帶的 Demo(AppWidget_FlashLight.rar)。
Android drawable 資料夾怎麼選?一次搞懂 mdpi、hdpi 與圖片縮放的關係
寫一個小遊戲,程式碼檢查了三遍都沒發現問題,可主界面上有幾張圖片就是莫名其妙地比設計稿大了一號。最後的解法荒唐得讓人哭笑不得——把圖片從 drawable-mdpi 換到
Android CardView 卡片式介面實作:打造支付寶風格銀行卡
CardView 的定位 CardView 是 Android 5.0 開始提供的卡片式容器控制項,本質上可視為一個帶有陰影與圓角能力的 FrameLayout 子類。它
Android 版「QQ 桌面」值得一試的實用小技巧
騰訊把「手機 QQ 桌面」帶到了 Android 平台。這個版本除了關注城市天氣與資訊熱點,還能在桌面直接收發 QQ 訊息,並打造屬於自己的待機桌面。這篇就來看看新版手機
MSM8909 + Android 5.1.1 開機流程(七):boot_linux_from_mmc() 逐段解析
先名詞解釋:FFBM(Fast Factory Boot Mode)是快速工程開機模式,讓工廠測試用的 APK 能提早啟動,不必等整個 Android 開機完成。 本篇要