前言
RemoteViews從字面上了解是遠端View,這個了解可能有點抽象,我們聽過遠端服務,但是遠端View聽說過的Android開發者應該很少,其實遠端View和遠端Service是一樣的。谷歌設計這個View的主要目的是為了跨程序更新界面,基于這個前提我們在Android裝置上這用得到RemoteViews的應用場景主要有兩個地方:通知欄和桌面小部件,我打算用三篇文章去了解RemoteViews,第一篇介紹RemoteViews的使用場景。第二篇是分析RomoteViews的内部運作機制,第三篇則是分析RomoteViews的意義和跨程序更新界面的場景。
通知欄裡的RemoteViews
系統通知欄我們應該很了解,這個是APP促活的一個關鍵手段,通過定時或活動時彈出Notification讓使用者點選促進App的使用者粘性,同時也可以讓使用者檢視某些功能的狀态,但是另一個方面來說這個功能使用很多時候會打擾使用者,讓使用者不堪其擾,當然這是題外話,技術永遠是為了業務服務的。回到主題,RemoteViews在通知欄上的應用可以有兩種狀态,一個是使用系統預設效果,另一個是自定義布局。
使用系統預設布局的代碼比較簡單,我這裡直接列出代碼:
val intent = Intent(this, NotificationOpenActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val channelId= "channelId"
val channelName = "channelName"
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
var notification = NotificationCompat.Builder(this, channelId)
.setContentTitle("notification_title")
.setContentText("notification_content")
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setContentIntent(pendingIntent)
.build()
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
manager.notify(1, notification)
上述代碼就可以彈出一個系統預設樣式的通知,點選通知則清除通知,并跳轉至NotificationOpenActivity,當發送通知欄的時候APP啟動icon右上角也會有數量标注。
在實際項目開發中我們往往需要自定義通知欄布局,這個也比較簡單,我們隻需要自定義一個布局檔案,然後利用RemoteViews來加載布局檔案就可以更改通知欄的樣式,自定義通知欄代碼如下:
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_notification"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_toRightOf="@id/iv_notification" />
</RelativeLayout>
頁面邏輯:
val openActivityPendingIntent = PendingIntent.getActivity(
this,
0,
Intent(this, NotificationOpenActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val channelId = "channelId"
val channelName = "channelName"
val channel =
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
val remoteViews = RemoteViews(packageName, R.layout.view_notification)
remoteViews.setTextViewText(R.id.tv_content, "自定義通知欄文字")
remoteViews.setImageViewResource(R.id.iv_notification, R.drawable.icon_notification)
remoteViews.setOnClickPendingIntent(R.id.btn_custom_notificateion, openActivityPendingIntent)
val notification = NotificationCompat.Builder(this, channelId)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_LIGHTS)
.setContentIntent(openActivityPendingIntent)
.setCustomContentView(remoteViews)
.build()
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
manager.notify(1, notification)
效果如下:
以上就是RemoteViews在通知欄的使用,使用起來相對簡單,隻要提供目前應用的包名和布局檔案的資源id就可以建構一個RemoteViews對象,通過布局内部子id
remoteViews.setTextViewText(R.id.tv_content, "自定義通知欄文字")
remoteViews.setImageViewResource(R.id.iv_notification, R.drawable.icon_notification)
就可以對文字和圖檔進行設定,然後RemoteViews點選事件方法則為:
這裡更新RemoteViews或許大家會覺得複雜,為什麼RemoteViews沒有提供類似View的findViewById這個方法呢,提供這個方法不就能擷取RemoteViews裡 的子View了,操作豈不是更簡單,RomoteViews的内部運作機制會在第二篇文章中進行了解,大家可以留個意。
桌面小部件上的RemoteViews
桌面小部件在Android開發裡的實作類是AppWidgetProvider,它本質上來說是一個廣播(BroadcastReceiver),因為這個類繼承的就是BroadcastReceiver。要了解RemoteViews在桌面小部件的應用我們需要先了解桌面小部件的使用,下面我們一步步做一個桌面小部件。
1:界面布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_app_widget"
android:layout_width="wrap_content"
android:src="@drawable/icon_notification"
android:layout_height="wrap_content" />
</LinearLayout>
這個layout布局沒什麼好說的,Android開發者都知道。
2:建立xml檔案夾并建立一個桌面小部件配置資訊檔案,我這裡這個配置資訊檔案命名為:app-widget_provider_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/view_app_widget"
android:minWidth="80dp"
android:minHeight="80dp"
android:updatePeriodMillis="10000"
>
</appwidget-provider>
這裡的配置檔案含義也比較明顯,分别是初始化布局和小部件最小寬高,值得說的是
這個參數的含義是定義小部件自動更新的周期,機關是毫秒,每個周期之後都會觸發小部件的自動更新。
3:定義小部件的廣播接收者
代碼如下:
package com.sjr.remoteviewsdemo
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.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.os.SystemClock
import android.util.Log
import android.widget.RemoteViews
import android.widget.Toast
/**
* Created by sjr on 2020/5/1
*/
class MyAppWidgetProvider : AppWidgetProvider() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (intent.action == CLICK_ACTION) {
Toast.makeText(context, "clicked it", Toast.LENGTH_SHORT).show()
Thread(Runnable {
val srcbBitmap = BitmapFactory.decodeResource(
context.resources, R.drawable.icon_notification
)
val appWidgetManager = AppWidgetManager.getInstance(context)
for (i in 0..36) {
val degree = (i * 10 % 360).toFloat()
val remoteViews = RemoteViews(
context
.packageName, R.layout.view_app_widget
)
remoteViews.setImageViewBitmap(
R.id.iv_app_widget,
rotateBitmap(srcbBitmap, degree)
)
val intentClick = Intent()
intentClick.action = CLICK_ACTION
val pendingIntent = PendingIntent
.getBroadcast(context, 0, intentClick, 0)
remoteViews.setOnClickPendingIntent(R.id.iv_app_widget, pendingIntent)
appWidgetManager.updateAppWidget(
ComponentName(
context, MyAppWidgetProvider::class.java
), remoteViews
)
SystemClock.sleep(30)
}
}).start()
}
}
override fun onUpdate(
context: Context, appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
val counter = appWidgetIds.size
for (i in 0 until counter) {
val appWidgetId = appWidgetIds[i]
onWidgetUpdate(context, appWidgetManager, appWidgetId)
}
}
private fun onWidgetUpdate(
context: Context,
appWidgeManger: AppWidgetManager, appWidgetId: Int
) {
val remoteViews = RemoteViews(
context.packageName,
R.layout.view_app_widget
)
// 視窗小部件點選事件發送的Intent廣播
val intentClick = Intent()
intentClick.action = CLICK_ACTION
val pendingIntent = PendingIntent.getBroadcast(
context, 0,
intentClick, 0
)
remoteViews.setOnClickPendingIntent(R.id.iv_app_widget, pendingIntent)
appWidgeManger.updateAppWidget(appWidgetId, remoteViews)
}
private fun rotateBitmap(srcbBitmap: Bitmap, degree: Float): Bitmap {
val matrix = Matrix()
matrix.reset()
matrix.setRotate(degree)
return Bitmap.createBitmap(
srcbBitmap, 0, 0,
srcbBitmap.width, srcbBitmap.height, matrix, true
)
}
companion object {
val CLICK_ACTION = "com.sjr.remoteviewsdemo.action.CLICK"
}
}
以上代碼就實作了一個簡單的桌面小部件,小部件顯示一張圖檔,将下夠不見添加至桌面之後,點選小部件小部件會旋轉一周,可以看到小部件的布局更新是通過RemoteViews來實作的。
4:在AndroidManifest.xml中聲明小部件
<receiver android:name=".MyAppWidgetProvider" >
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider_info" >
</meta-data>
<intent-filter>
<action android:name="com.sjr.remoteviewsdemo.action.CLICK" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
</receiver>
四大元件都需要在AndroidManifest中聲明,桌面小部件本質上也是一個BroadcastReceiver,是以也需要在此進行注冊,上面的小部件一共有兩個action,一個是用來識别小部件的點選,一個是系統規定作為小部件必須有的辨別。
AppWidgetProvider的onREceive方法的分發源碼如下:
/**
* Implements {@link BroadcastReceiver#onReceive} to dispatch calls to the various
* other methods on AppWidgetProvider.
*
* @param context The Context in which the receiver is running.
* @param intent The Intent being received.
*/
// BEGIN_INCLUDE(onReceive)
public void onReceive(Context context, Intent intent) {
// Protect against rogue update broadcasts (not really a security issue,
// just filter bad broacasts out so subclasses are less likely to crash).
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null && appWidgetIds.length > 0) {
this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
}
}
} else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
this.onDeleted(context, new int[] { appWidgetId });
}
} else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
&& extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) {
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context),
appWidgetId, widgetExtras);
}
} else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
this.onEnabled(context);
} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
this.onDisabled(context);
} else if (AppWidgetManager.ACTION_APPWIDGET_RESTORED.equals(action)) {
Bundle extras = intent.getExtras();
if (extras != null) {
int[] oldIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
int[] newIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (oldIds != null && oldIds.length > 0) {
this.onRestored(context, oldIds, newIds);
this.onUpdate(context, AppWidgetManager.getInstance(context), newIds);
}
}
}
}
由以上源碼可以看到,根據不同的Action,OnReceive會調用onEnable、onDisable、onUpdate
下面逐個說明:
- onEnable:當小部件第一次添加到桌面的時候會調用這個方法,添加到桌面可以多次,但是這個方法隻會被調用一次;
- onUpdate:小部件被點選或者周期間隔會調用這個方法;
- onDelete :每删除一次桌面小部件就會調用一次;
- onDisabled:當最後一個該類型的小部件被删除時會調用這個方法;
- onREceive:分發具體的事件給各個方法;
到這裡就是整個桌面小部件開發的流程了,從整個流程我們可以發現,桌面小部件的操作都是通過RemoteViews來完成的,不管是初始化還是更新。
小結
通過這兩個RemoteViews的應用我們初步了解了RemoteViews這個遠端View,由于篇幅有限,是以不會在一篇内了解完RemoteViews,下一篇文章會分析RemoteViews的機制。