前言
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 =