編輯:Android 開發實例
在 AppWidget 中,Button 與 ImageButton 都是被官方支援的元件,而它們的點擊事件可以歸納為三種類型:開啟 Activity、啟動 Service、發送自訂 Action 廣播。三者共用同一套骨架:建立 Intent → 包裝成 PendingIntent → 綁到 RemoteViews → 推回畫面。差別只在於 PendingIntent 用哪個方法取得。
不論哪一種,最後兩步都一樣:
// 3. 對應小工具版面的 RemoteViews RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget); // 4. 為按鈕綁定事件 views.setOnClickPendingIntent(R.id.IBfullscreen, somePendingIntent); // 5. 更新小工具 appWidgetManager.updateAppWidget(appWidgetIds, views);
第 5 步若在 onUpdate() 內部進行,可直接使用傳入的 appWidgetManager 與 appWidgetIds。若是在 onUpdate() 之外(通常是 Service 中)更新,則需自行準備三個物件:
public RemoteViews views; // RemoteViews 物件 public ComponentName thisWidget; // 組件名稱 public AppWidgetManager manager; // AppWidget 管理器 thisWidget = new ComponentName(this, PictureAppWidgetProvider.class); manager = AppWidgetManager.getInstance(this); manager.updateAppWidget(thisWidget, views);
改用 ComponentName 版本的好处是:不必知道具體的小工具 ID,一次就能更新該 provider 的所有實例。
// 1. 建立要開啟的 Intent,需要傳值時用 putExtra
Intent fullIntent = new Intent(this, FullScreen.class);
fullIntent.putExtra("isCircle", isCircle);
// 2. 包裝成 PendingIntent
// 帶有附加資料時,flag 建議用 FLAG_CANCEL_CURRENT
PendingIntent pFullIntent = PendingIntent.getActivity(
this, 0, fullIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// 不帶資料時,flag 可傳 0
PendingIntent pSimple = PendingIntent.getActivity(this, 0, fullIntent, 0);
// 3~5 依前述共通骨架處理
views.setOnClickPendingIntent(R.id.IBfullscreen, pFullIntent);
關於 flag:當 PendingIntent 已存在且帶有新的 extras 時,若不使用 FLAG_CANCEL_CURRENT,系統可能沿用舊的 extras 而不更新,這是「按了按鈕卻拿到舊資料」的常見原因。
// 1. 以 Action 指定要啟動的 Service
Intent startServiceIntent = new Intent("zyf.temp.Service.START");
// 2. 用 getService 取得 PendingIntent
PendingIntent pIntent =
PendingIntent.getService(context, 0, startServiceIntent, 0);
// 3~5 依前述共通骨架處理
views.setOnClickPendingIntent(R.id.IBfullscreen, pIntent);
這種寫法很適合「按一下開始播放」「按一下開始下載」這類需要持續背景工作的情境。
// 1. 定義自訂 Action
Intent prevIntent = new Intent("PREV");
// 2. 用 getBroadcast 送出廣播
PendingIntent pPrevIntent =
PendingIntent.getBroadcast(this, 0, prevIntent, 0);
// 3~5 依前述共通骨架處理
views.setOnClickPendingIntent(R.id.IBprev, pPrevIntent);
有兩個地方可以攔截:
做法 A:在 AppWidgetProvider 的 onReceive() 中接收
先在 Manifest 的 intent-filter 中註冊 Action(系統的 APPWIDGET_UPDATE 必須保留,否則 onUpdate 不會被觸發):
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="PREV" />
</intent-filter>
接著在 onReceive() 中分派:
if (intent.getAction().equals("PREV")) {
// 處理自訂事件
} else {
super.onReceive(context, intent);
}
再次提醒:else 中的 super.onReceive() 必須存在,否則系統派發的生命週期方法不會被呼叫;但也不能把它寫到 if 之外,否則每個事件都會被處理兩次。
做法 B:在 Service 中註冊 BroadcastReceiver
IntentFilter filter = new IntentFilter();
filter.addAction("PREV");
registerReceiver(doCommand, filter);
// 在 BroadcastReceiver 的 onReceive 中處理
if (intent.getAction().equals("PREV")) {
// 處理自訂事件
}
記得在 Service 銷毀時呼叫 unregisterReceiver(),避免洩漏。
· 只是要跳到 App 內的某個畫面:用 getActivity。
· 需要一段持續的背景工作(播放、下載、同步):用 getService。
· 只是要更新小工具自身的顯示狀態(換頁、切歌、計數):用 getBroadcast,成本最低、也最常見。
Android Fragment 雙欄布局實例解析
Fragment 的雙欄概念 Fragment 是 Android 3.0 之後引入的介面元件,適合將畫面拆成可重複使用的區塊。常見用法是左側顯示列表,右側顯示內容;在平
Android 進階:藍牙傳感應用實例
前文介紹過 Android 利用麥克風采集並顯示模擬信號的方法,這種方式適用於沒有 IO 控制、單純讀取信號的場景。但如果傳感器本身還需要控制電路(例如採集血氧信號時要紅
三分鐘做出 Android 登入頁與失敗次數限制
登入畫面是所有需要帳號的 App 的第一道門。它看起來簡單,實際上要顧及的地方不少:密碼不能被看見、錯誤要有清楚提示、以及最重要的——不能讓人無限次猜下去。 本文用最精
Android 登入頁面教學:帳密驗證與剩餘次數提示
登入頁是許多 App 的第一道門。它向使用者索取一組憑證,換取進入的權限——不論是社群平台、雲端硬碟還是公司内部系统,邏輯都相同。 本章要動手做一個登入介面,並一併處理實