android remoteViews

## 1、RemoteViews介绍

- remoteviews是一个远程view,是运行在另外一个线程里的view

- 可以跨进程更新view

- 主要应用在通知栏Notification和桌面小部件中

## 2、RemoteViews在通知栏中的运用

- 废话不多说,直接上代码

 private fun openRemoteViewNotification() {
        val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //8.0 以后需要加上channelId 才能正常显示
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channelId = "default"
            val channelName = "默认通知"
            manager.createNotificationChannel(NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH))
        }
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(this, "default")
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85"))
        val pendingIntent = PendingIntent.getActivities(this, 0, arrayOf(intent), 0)
        val notification = builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
            .setAutoCancel(true)
            .setContentTitle("普通通知")
            .build()

        val remoteViews = RemoteViews(packageName, R.layout.layout_notification_new)
        remoteViews.setTextViewText(1, "chapter_5: ")
        remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher)
        val openActivity2PendingIntent = PendingIntent.getActivity(this,
                0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT)
        remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2PendingIntent)
        notification.contentView = remoteViews
        notification.contentIntent = pendingIntent
        manager.notify(1, notification)
    }

 

## 3、RemoteViews机制

RemoteView没有findViewById方法,因此无法访问里面的View元素,而必须通过RemoteViews所提供的一系列set方法来完成,这是通过反射调用的

通知栏和小组件分别由NotificationManager(NM)和AppWidgetManager(AWM)管理,而NM和AWM通过Binder分别和SystemService进程中的NotificationManagerService以及AppWidgetService中加载的,而它们运行在系统的SystemService中,这就和我们进程构成了跨进程通讯。

## 4、支持组件

布局:FrameLayout、LinearLayout、RelativeLayout、GridLayout

组件:Button、ImageButton、ImageView、ProgressBar、TextView、ListView、GridView、ViewStub等(例如EditText是不允许在RemoveViews中使用的,使用会抛异常)。

## 5、原理

系统将view操作封装成Action对象,Action同样实现了Parcelable接口,通过Binder传递到SystemServer进程。远程进程通过RemoteViews的apply方法来进行view的更新操作,RemoteViews的apply方法内部则会去遍历所有的action对象并调用它们的apply方法来进行view的更新操作。

这样做的好处是不需要定义大量的Binder接口,其次批量执行RemoteViews中的更新操作提高了程序性能。

## 6、工作流程

首先RemoteViews会通过Binder传递到SystemService进程,因为RemoteViews实现了Parcelable接口,因此它可以跨进程传输,系统会根据RemoteViews的包名等信息拿到该应用的资源;然后通过LayoutInflater去加载RemoteViews中的布局文件。接着系统会对View进行一系列界面更新任务,这些任务就是之前我们通过set来提交的。set方法对View的更新并不会立即执行,会记录下来,等到RemoteViews被加载以后才会执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值