21-1-23:Android开发艺术探索读书笔记:理解RemoteViews

RemoteViews

远程View 和远程Service是一样的。RemoteViews表示的是一个View结构,可以在其他进程中息显示,即跨进程更新界面。使用场景有:通知栏和桌面小部件。

RemoteViews应用

用于自定义通知效果

                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    NotificationChannel channel = new NotificationChannel(channelId, "simple", NotificationManager.IMPORTANCE_DEFAULT);
                    manager.createNotificationChannel(channel);
                    Notification notification = new NotificationCompat.Builder(MainActivity.this, channelId)
                            .setContentTitle("This is content title")
                            .setContentText("This is content text")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                            .build();
                    RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.remote);
                    remoteViews.setTextViewText(R.id.msg,"MSGGGGGGGGGGGG");
                    remoteViews.setImageViewResource(R.id.myimage,R.drawable.picture);
                    PendingIntent pi = PendingIntent.getActivities(context,0, new Intent[]{new Intent(context, MainActivity2.class)},PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.open,pi);
                    notification.contentView = remoteViews;
                    notification.contentIntent = pi;
                    manager.notify(1, notification);

实际上无法获得remoteview本身的view视图,更新也要手动逐步实现
在这里插入图片描述

桌面上的小部件

AppWidgetProvider是用于实现桌面小部件的类 本质是一个广播。
小组件的具体实现

PendingIntent

区别于Intent 立即发生,PendingIntent是未来某一个时刻发生。使用的典型场景是remoteview 因为它无法直接向view设置点击事件,要通过PendingIntent,而PendingIntent又是通过send和cancel方法来发送和取消待定Intent

PendingIntent代表三种意图 启动活动 服务 广播
在这里插入图片描述在这里插入图片描述
flag位置有四个 决定了接收到相同的通知会呈现什么样式。

Remoteview内部机制

主要作用是在其他进程中显示并且更新view。
在这里插入图片描述
小部件 appWidgetManager 窗口通知 NotificationManager
都通过binder和SystemServer进程进行通信。因为属于系统的进程 所以属于进程通信
系统可以通过Binder支持所有的view操作 但是代价太大 。所以提供了一个action的概念,action代表一个view操作 实现了parcelable接口
内部定义了一个Action的List 远程得到这个列表(RemoteView对象)之后遍历每个对象并且调用apply方法。初始化界面调用apply,后续更新会调用reapply更新界面

Remoteview意义

跨进程通信 代替AIDL 缺点是适配的View种类太少

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值