Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

安卓學習筆記:深入掌握 RemoteViews 的原理與應用

編輯:關於 Android 程式設計

一、RemoteViews 的兩種應用

1. 桌面 Widget 小部件;
2. 通知欄通知信息。

通知欄通知示例

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
    remoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_item_dl);
    remoteViews.setTextViewText(R.id.tv_filename, getPackageName()); // 設置標題
    Intent btIntent = new Intent();
    btIntent.setAction("com.yu.sendnotification.action_dl");
    PendingIntent btPendingIntent = PendingIntent.getBroadcast(this, 1, btIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.bt_stop, btPendingIntent); // 設置點擊事件
    Intent intent = new Intent(this, NewActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    noti = new Notification.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
        .setTicker("Hello notification")
        .setWhen(System.currentTimeMillis())
        .setAutoCancel(true)
        .setContentIntent(pi).build();
    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, noti);
}

通知欄使用步驟

1. 透過 Notification.Builder 創建 builder 對象;
2. 給 builder 設置相關選項,常用方法有:

setContent(remoteViews) 設置 RemoteViews;setContentIntent(intent) 設置 view 的 intent 事件;setContentText() 設置內容文字;setContentTitle() 設置標題文字;setAutoCancel(bool) 設置自動取消;setSmallIcon() 設置小圖標;setTicker() 設置狀態欄滾動文字;setDeleteIntent() 設置用戶滑動刪除通知的 intent;setProgress() 設置進度;setWhen() 設置發送時間;setSound() 設定鈴聲;setLights() 設定 LED 燈閃爍;setVibrate() 設定震動模式;setDefaults() 設定默認響應(DEFAULT_ALL、DEFAULT_SOUND、DEFAULT_VIBRATE、DEFAULT_LIGHTS)。

需要權限:

<!-- 閃光燈權限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 振動器權限 -->
<uses-permission android:name="android.permission.VIBRATE" />

3. builder.build() 創建 Notification 對象;
4. 獲取 NotificationManager 並發送通知 manager.notify(0, noti);

二、PendingIntent —— 即將發生的意圖

作用

1)Intent 是即時發生的意圖,PendingIntent 是不確定發生時間的意圖;
2)用於給 RemoteViews 添加點擊事件;
3)支持啟動 Activity、啟動 Service、發送廣播三種意圖。

三種意圖參數

(Context context, int requestCode, Intent intent, int flags),其中 requestCode 表示請求碼,flags 為標志,intent 為意圖。

flags 參數的意義

1. FLAG_CANCEL_CURRENT:若系統中已存在相同的 PendingIntent,先取消已有的,再重新生成一個。
2. FLAG_NO_CREATE:若系統中不存在相同的 PendingIntent,則不創建,直接返回 null。
3. FLAG_ONE_SHOT:該 PendingIntent 只作用一次,send() 觸發後會自動 cancel(),再次調用 send() 會拋出 SendIntentException。
4. FLAG_UPDATE_CURRENT:若存在對等的 PendingIntent,則使用它,但會用新的 Intent 更新其數據(如 Extras)。

匹配規則

兩個 PendingIntent 相同的條件:requestCode 與 Intent 都相同。Intent 相同指 Component 與 intent-filter 相同(與 Extras 無關)。

id 與 flags 對通知更新的影響

manager.notify(id, notification) 發通知:當 id 相同時,後面的通知覆蓋原來;當 id 不同時,若 PendingIntent 不匹配則通知互不干擾、不受 flags 影響;若匹配則按 flags 分析:FLAG_ONE_SHOT 會讓後續 PendingIntent 與第一條保持一致(包括 Extras),單擊任意一條後其他無法打開;FLAG_CANCEL_CURRENT 只有最新通知可打開;FLAG_UPDATE_CURRENT 會更新之前所有通知的 PendingIntent(包括 Extras),且都可打開。

三、RemoteViews 的內部原理

常用構造函數

public RemoteViews(String packageName, int layoutId)

支持的 View 類型(不支持自定義 View)

layout:FrameLayout、LinearLayout、RelativeLayout、GridLayout;
View:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView。

常用 set 方法(跨進程顯示 View,無法用 findViewById)

setTextViewText() 設置文本;setTextViewTextSize() 設置字體大小;setTextColor() 設置顏色;setImageViewResource() 設置圖片;setInt()/setLong()/setBoolean() 透過反射調用 View 的對應類型方法;setOnClickPendingIntent() 添加點擊事件(事件類型只能為 PendingIntent)。大部分方法都是透過反射調用 View 的方法。

內部機制

運行環境

NotificationManager 與 AppWidgetManager 透過 Binder 分別和運行於 SystemServer 的 NotificationManagerService 與 AppWidgetManagerService 通信,通知欄和桌面小部件的布局分別在它們中被加載。

執行流程

1. RemoteViews 實現了 Parcelable 接口,可跨進程傳輸至 SystemServer;
2. 系統根據 packageName、layoutId 得到該應用的資源,透過 LayoutInflater 加載布局檔;
3. 系統執行一系列更新操作,但不是立即執行,而是記錄所有更新操作,待 RemoteViews 加載後執行;
4. 顯示後再次更新時,調用 set 方法並透過 NotificationManager 與 AppWidgetManager 提交更新任務,具體更新在 SystemServer 中完成;
5. 每次 set 操作對應一個 action(實現了 Parcelable),當更新提交時,這些 action 會被跨進程傳輸並在遠程進程執行。遠程進程透過 apply 方法遍歷並執行所有 action,通知欄和桌面小插件初始化時調用 apply,後續更新調用 reapply。

單擊事件的使用

RemoteViews 只支持發起 PendingIntent,不支持 OnClickListener 模式。setPendingIntentTemplate 與 setOnClickFillInIntent 須結合使用,給 ListView 和 StackView 中的 item 添加點擊事件。

使用 RemoteViews 的好處

不需要定義大量 Binder 接口;透過在遠程進程批量執行修改操作,避免大量 IPC 操作,提高性能;只需操作 RemoteViews 即可實現效果,簡單易用。

四、AIDL 與 RemoteView 的應用場景

1. 一個應用簡單地更新另一個應用的界面,可選擇 AIDL;但若界面更新頻繁,會有效率問題,且 AIDL 接口可能使問題複雜化,此時不宜使用 AIDL。
2. 界面更新頻繁時,適宜採用 RemoteView 實現,缺點是僅支持一些常見 View,不支持自定義 View。

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