Android 关于RemoteViews的理解(一)
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右上角也会有数量标注。
页面逻辑:
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)
效果如下:
这个layout布局没什么好说的,Android开发者都知道。
2:新建xml文件夹并新建一个桌面小部件配置信息文件,我这里这个配置信息文件命名为:app-widget_provider_info.xml
这里的配置文件含义也比较明显,分别是初始化布局和小部件最小宽高,值得说的是
android:updatePeriodMillis="10000"
这个参数的含义是定义小部件自动更新的周期,单位是毫秒,每个周期之后都会触发小部件的自动更新。
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
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 class="lazy" data-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(class="lazy" data-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(class="lazy" data-srcbBitmap: Bitmap, degree: Float): Bitmap {
val matrix = Matrix()
matrix.reset()
matrix.setRotate(degree)
return Bitmap.createBitmap(
class="lazy" data-srcbBitmap, 0, 0,
class="lazy" data-srcbBitmap.width, class="lazy" data-srcbBitmap.height, matrix, true
)
}
companion object {
val CLICK_ACTION = "com.sjr.remoteviewsdemo.action.CLICK"
}
}
以上代码就实现了一个简单的桌面小部件,小部件显示一张图片,将下够不见添加至桌面之后,点击小部件小部件会旋转一周,可以看到小部件的布局更新是通过RemoteViews来实现的。
四大组件都需要在AndroidManifest中声明,桌面小部件本质上也是一个BroadcastReceiver,所以也需要在此进行注册,上面的小部件一共有两个action,一个是用来识别小部件的点击,一个是系统规定作为小部件必须有的标识。
AppWidgetProvider的onREceive方法的分发源码如下:
// 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
下面逐个说明:
到这里就是整个桌面小部件开发的流程了,从整个流程我们可以发现,桌面小部件的操作都是通过RemoteViews来完成的,不管是初始化还是更新。
小结通过这两个RemoteViews的应用我们初步了解了RemoteViews这个远程View,由于篇幅有限,所以不会在一篇内了解完RemoteViews,下一篇文章会分析RemoteViews的机制。
准备下雨了 原创文章 17获赞 14访问量 4万+ 关注 私信 展开阅读全文作者:准备下雨了
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341