前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RemoteView

RemoteView

作者头像
提莫队长
发布2019-02-21 11:21:03
1.1K0
发布2019-02-21 11:21:03
举报
文章被收录于专栏:刘晓杰刘晓杰

使用方法我就不介绍了,网上一大堆

1.PendingIntent概述

PendingIntent表示在将来的某个时刻发生,Intent是立即发生。 PendingIntent的匹配规则:如果两个PendingIntent它们内部的Intent相同并且requestCode也相同,那么这两个PendingIntent就是相同的(如果两个Intent的ComponentName和intent-filter都相同,那么这两个Intent就是相同的,Extras不参与Intent匹配)。

PendingIntent支持三种待定意图

这里写图片描述
这里写图片描述

第一个和第三个参数好理解,第二个参数requestCode表示PendingIntent发送方的请求码,通常为0.另外requestCode会影响后面flag的取值。 flag有如下取值:

这里写图片描述
这里写图片描述

2.RemoteViews的内部机制

进入源码

代码语言:javascript
复制
class RemoteViews implements Parcelable, Filter

它实现了Parcelable接口,显然和跨进程通信有关 然后看一下最常用的构造方法

代码语言:javascript
复制
public RemoteViews(String packageName, int layoutId)

packageName表示当前应用的包名 layoutId对应的布局文件 RemoteViews只支持如下类型

代码语言:javascript
复制
Layout:
FrameLayout,LinearLayout,RelativeLayout,GridLayout
View:
AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar,TextView,ViewFlipper,ListView,GridView,StackView,AdapterViewFlipper,ViewStub

首先讲讲内部实现机制

这里写图片描述
这里写图片描述

通知栏和桌面小部件分别由NotificationManager和AppWidgetManager管理。而NotificationManager和AppWidgetManager通过binder分别和SystemServer进程中的NotificationManagerService和AppWidgetService进行通信。由此可见通知栏和桌面小部件是在NotificationManagerService和AppWidgetService被加载,然后运行在SystemServer中

首先RemoteViews通过Binder传递到SystemServer中(实现了Parcelable接口,可以跨进程传输)。系统根据包名得到资源,然后通过layoutinflater加载布局文件。(在SystemServer算一个普通的view,而在我们的进程中算RemoteViews)然后会有一系列set方法更新view,但不会立即执行,而是会保存下来,直到RemoteViews被加载以后再执行。

理论上来讲,系统可以让binder直接支持所有操作,但是大量IPC操作会影响效率

由于RemoteViews是在远程进程中显示,所以无法用findviewbyid,通常用set方法,比如setTextViewText()。接下来看一下它的源码

代码语言:javascript
复制
    public void setTextViewText(int viewId, CharSequence text) {
        setCharSequence(viewId, "setText", text);
    }

再看一下setCharSequence源码

代码语言:javascript
复制
    public void setCharSequence(int viewId, String methodName, CharSequence value) {
        addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value));
    }

再看一下addAction源码

代码语言:javascript
复制
    private void addAction(Action a) {
        if (hasLandscapeAndPortraitLayouts()) {
            throw new RuntimeException("RemoteViews specifying separate landscape and portrait" +
                    " layouts cannot be modified. Instead, fully configure the landscape and" +
                    " portrait layouts individually before constructing the combined layout.");
        }
        if (mActions == null) {
            mActions = new ArrayList<Action>();
        }
        mActions.add(a);

        // update the memory usage stats
        a.updateMemoryUsageEstimate(mMemoryUsageCounter);
    }

这下可以看到里面有一个ArrayList专门用来存储Action的。但是显然现在只是把Action保存起来,并没有对view进行实际操作啊。之前说过了“会保存下来,知道RemoteViews被加载以后再执行” 接下来看一个apply函数

代码语言:javascript
复制
    public View apply(Context context, ViewGroup parent, OnClickHandler handler) {
        RemoteViews rvToApply = getRemoteViewsToApply(context);

        View result;
        final Context contextForResources = getContextForResources(context);
        Context inflationContext = new ContextWrapper(context) {
            @Override
            public Resources getResources() {
                return contextForResources.getResources();
            }
            @Override
            public Resources.Theme getTheme() {
                return contextForResources.getTheme();
            }
        };

        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        inflater = inflater.cloneInContext(inflationContext);
        inflater.setFilter(this);
        result = inflater.inflate(rvToApply.getLayoutId(), parent, false);

        rvToApply.performApply(result, parent, handler);

        return result;
    }

apply先加载resources,然后解析remoteview的布局,最后调用performApply

代码语言:javascript
复制
    private void performApply(View v, ViewGroup parent, OnClickHandler handler) {
        if (mActions != null) {
            handler = handler == null ? DEFAULT_ON_CLICK_HANDLER : handler;
            final int count = mActions.size();
            for (int i = 0; i < count; i++) {
                Action a = mActions.get(i);
                a.apply(v, parent, handler);
            }
        }
    }

可以看到,这里才是对view的真正操作

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年08月26日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.PendingIntent概述
  • 2.RemoteViews的内部机制
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档