RemoteView

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

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的内部机制

进入源码

class RemoteViews implements Parcelable, Filter

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

public RemoteViews(String packageName, int layoutId)

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

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()。接下来看一下它的源码

    public void setTextViewText(int viewId, CharSequence text) {
        setCharSequence(viewId, "setText", text);
    }

再看一下setCharSequence源码

    public void setCharSequence(int viewId, String methodName, CharSequence value) {
        addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value));
    }

再看一下addAction源码

    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函数

    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

    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的真正操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RemoteViews 是 Android 中的一个特殊类,用于在一个进程中创建布局并将其在另一个进程中显示。它是用于在应用程序之间共享布局和视图的一种机制。 使用 RemoteViews,你可以创建一个布局,并将其传递给另一个进程,以便在该进程中显示。这在 Android 中的一些场景中非常有用,比如创建小部件、通知和锁屏界面。 要使用 RemoteViews,首先需要创建一个布局文件,就像你在普通的视图中所做的那样。然后,使用 RemoteViews 实例化一个对象,并设置要在布局中显示的视图和数据。 下面是一个简单的示例,演示如何使用 RemoteViews 创建一个包含 TextView 的布局,并将其在另一个进程中显示: ```java // 创建 RemoteViews 实例 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_layout); // 设置 TextView 的文本 remoteViews.setTextViewText(R.id.text_view, "Hello World!"); // 使用 AppWidgetManager 更新小部件 AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews); ``` 这是一个基本的示例,你可以根据需要自定义布局和设置其他视图属性。 需要注意的是,RemoteViews 支持的视图类型有限,不支持所有的 Android 视图。你可以在官方文档中查看 RemoteViews 支持的视图类型。 希望这个简单的介绍能够帮助你了解 RemoteViews 的基本用法。如果你有更多的问题,欢迎继续提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值