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

AppWidget 按鈕事件三種型態:開啟 Activity、啟動 Service 與發送 Action

編輯: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 的所有實例。

型態一:開啟 Activity

// 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 而不更新,這是「按了按鈕卻拿到舊資料」的常見原因。

型態二:啟動 Service

// 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);

這種寫法很適合「按一下開始播放」「按一下開始下載」這類需要持續背景工作的情境。

型態三:發送自訂 Action

// 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);

如何接收這個 Action

有兩個地方可以攔截:

做法 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,成本最低、也最常見。

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