到目前为止,我发现RemoteView会用在两个地方:一个是在AppWidget,另外一个是在Notification.

先从官方对他的定义来看: 
RemoteView-- 
A class that describes a view hierarchy that can be displayed in another process. The hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.

RemoteView描述一个view,而这个view是在另外一个进程显示的。它inflate于layout资源文件。并且提供了可以修改过view内容的一些简单基础的操作。

从这个定义我们就知道RemoteView是用来描述一个垮进程显示的view。从而你就会明白为什么AppWidget和Nofication需要用到它了。 
1,AppWidget---RemoteView 
我们都知道AppWidgetProvider是一个BrocaseReceiver,只是接受到Enable, Update,disale,delete这些message,而真正显示界面的是AppWidgetHostView(这是在Launcher里面实现的)这中间就是通过RemoteView来沟通。通过RemoteView告诉Launcher你想要的AppWidget是长什么样。

2,Notification--RemoteView

若你想自定义你的Notification也必须通过RemoteView.因为你定义的Nofication和显示Notification也是两个不同的进程。

如何实例化一个RemoteViews

构造方法 
RemoteViews(String packageName, int layoutId) 
创建一个新的RemoteViews 对象将显示 views包含指定一个layout资源. 
RemoteViews(Parcel parcel) 
读取RemoteViews对象从一个parcel中.

首先给大家一段例子简单说明下构造和如何使用

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider); 
     views.setTextViewText(R.id.appwidget_text, "Android开发网欢迎您");

appWidgetManager.updateAppWidget(appWidgetId, views);

RemoteViews中的setxxx方法 
比如setCharSequence(int viewId, String methodName, CharSequence value); 
views.setString(R.id.textview01, "setText", battery + "%"); 
其中views是RomoteViews的实例, 
第一个参数就是ID了, 
第二个参数,是一个方法名字,比如这里是textView,那么textView会有很多方法,比如setBackground(), setText(), setTextColor()等等,第二个参数就填这个函数名,不要括号, 
第三个参数就填第二个函数所用到的参数,比如如果是setTextColor(int), 第三个参数就带int进去(当然如果是这个你就必须用views.setInt(...)这个函数)

详细的 该类的公共方法列表,下面的viewId为layout文件中的id定义,常用的方法已经翻译成中文描述。

Public Methods 
View apply(Context context, ViewGroup parent) 
Inflates the view hierarchy represented by this object and applies all of the actions. 
int describeContents() 
Describe the kinds of special objects contained in this Parcelable's marshalled representation. 
int getLayoutId() 
String getPackage() 
boolean onLoadClass(Class clazz) 
Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed to be inflated. 
void reapply(Context context, View v) 
Applies all of the actions to the provided view. 
void setBitmap(int viewId, String methodName, Bitmap value) 
Call a method taking one Bitmap on a view in the layout for this RemoteViews. 
void setBoolean(int viewId, String methodName, boolean value) 
Call a method taking one boolean on a view in the layout for this RemoteViews. 
void setByte(int viewId, String methodName, byte value) 
Call a method taking one byte on a view in the layout for this RemoteViews. 
void setChar(int viewId, String methodName, char value) 
Call a method taking one char on a view in the layout for this RemoteViews. 
void setCharSequence(int viewId, String methodName, CharSequence value) 
Call a method taking one CharSequence on a view in the layout for this RemoteViews. 
void setChronometer(int viewId, long base, String format, boolean started) 
Equivalent to calling Chronometer.setBase, Chronometer.setFormat, and Chronometer.start() or Chronometer.stop(). 
void setDouble(int viewId, String methodName, double value) 
Call a method taking one double on a view in the layout for this RemoteViews. 
void setFloat(int viewId, String methodName, float value) 
Call a method taking one float on a view in the layout for this RemoteViews. 
void setImageViewBitmap(int viewId, Bitmap bitmap) 
等同于调用ImageView.setImageBitmap方法,从Bitmap对象中设置一个图片 
void setImageViewResource(int viewId, int srcId) 
等同于调用ImageView.setImageResource,从一个资源中设置图片 
void setImageViewUri(int viewId, Uri uri) 
等同于调用ImageView.setImageURI,从URI中设置图像 
void setInt(int viewId, String methodName, int value) 
Call a method taking one int on a view in the layout for this RemoteViews. 
void setLong(int viewId, String methodName, long value) 
Call a method taking one long on a view in the layout for this RemoteViews. 
void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) 
Equivalent to calling setOnClickListener(android.view.View.OnClickListener) to launch the provided PendingIntent. 
void setProgressBar(int viewId, int max, int progress, boolean indeterminate) 
等同于调用ProgressBar.setMax, ProgressBar.setProgress, and ProgressBar.如果indeterminate为true则进度条的最大和最小进度将会忽略 
void setShort(int viewId, String methodName, short value) 
Call a method taking one short on a view in the layout for this RemoteViews. 
void setString(int viewId, String methodName, String value) 
Call a method taking one String on a view in the layout for this RemoteViews. 
void setTextColor(int viewId, int color) 
等同于setTextColor(int).,设置文本的颜色 
void setTextViewText(int viewId, CharSequence text) 
等同于TextView.setText,设置文本内容 
void setUri(int viewId, String methodName, Uri value) 
Call a method taking one Uri on a view in the layout for this RemoteViews. 
void setViewVisibility(int viewId, int visibility) 
等同于调用View.setVisibility,设置该ID控件的可见性 
void writeToParcel(Parcel dest, int flags) 
Flatten this object in to a Parcel.

RemoteViews中的setxxx方法 
比如setCharSequence(int viewId, String methodName, CharSequence value); 
views.setString(R.id.textview01, "setText", battery + "%"); 
其中views是RomoteViews的实例, 
第一个参数就是ID了, 
第二个参数,是一个方法名字,比如这里是textView,那么textView会有很多方法,比如setBackground(), setText(), setTextColor()等等,第二个参数就填这个函数名,不要括号, 
第三个参数就填第二个函数所用到的参数,比如如果是setTextColor(int), 第三个参数就带int进去(当然如果是这个你就必须用views.setInt(...)这个函数)

Android之RemoteViews相关推荐

  1. android remoteviews 设置背景,Android通过RemoteViews实现跨进程更新UI示例

    一.概述 前面一篇文章Android通过AIDL实现跨进程更新UI我们学习了aidl跨进程更新ui,这种传统方式实现跨进程更新UI是可行的,但有以下弊端: View中的方法数比较多,在IPC中需要增加 ...

  2. Android View - RemoteViews

    设计Android的工程师起名字还是挺规范的,而且一眼就知道是什么意思.RemoteViews,顾名思义,远程的View.Android为了能让进程A显示进程B的View,设计了这么一种View(其实 ...

  3. Android中RemoteViews的实现

    田海立@CSDN 2012-8-22 本文结合AppWidget的应用场景,分析Android中RemoteViews的内部具体实现. 从前文<Android中AppWidget的分析与应用:A ...

  4. Android 关于RemoteViews的理解(一)

    前言 RemoteViews从字面上理解是远程View,这个理解可能有点抽象,我们听过远程服务,但是远程View听说过的Android开发者应该很少,其实远程View和远程Service是一样的.谷歌 ...

  5. Android 关于RemoteViews的理解(二)

    前言 上一篇文章<Android 关于RemoteViews的理解(一)>介绍了RemoteViews的使用场景,我们学习东西的时候要知其然知其所以然.我之前考虑一篇文章就可以说明Remo ...

  6. Android之RemoteViews篇上————通知栏和桌面小控件

    Android之RemoteViews篇上----通知栏和桌面小控件 一.目录 文章目录 Android之RemoteViews篇上----通知栏和桌面小控件 一.目录 二.RemoteViews的概 ...

  7. Android的RemoteViews

    能在另一个进程中显示页面布局的层次.层次是在布局文件中定义,它提供了一些改变布局文件的方法定义. 该类最重要的作用就是能够在另一个进程中使用!!! void addView(int viewId, R ...

  8. android remoteviews view,Android 理解RemoteViews

    导语 什么是远程view呢?它和远程service一样,RemoteViews可以在其他进程中显示.我们可以跨进程更新它的界面.在Android中,主要有两种场景:通知栏和桌面小部件. 本章先简单介绍 ...

  9. 【学习】Android的RemoteViews

    什么是RemoteViews 它表示的是一个View结构,可以在其他进程中显示,由于它在其他进程中显示,为了更新它的界面,它提供了一组基础的操作用于跨进程更新它的界面. RemoteViews的应用 ...

最新文章

  1. 盛大 Everbox同步网盘,可以本地和云服务文件同步,还不错,推荐下面的注册地址...
  2. 设计模式--简单工厂模式
  3. 深度学习(八)RBM受限波尔兹曼机学习-未完待续
  4. 【限时】推荐算法工程师培养计划
  5. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
  6. html正则表达式识别网址,JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)...
  7. 第三次实验及动手动脑
  8. 疯狂的html css,疯狂Html+CSS+JS 中JS总结
  9. 2021【第十二届蓝桥杯省赛】 C/C++ B组(第二场)
  10. echart重新加载数据_在asp.net core中动态加载最新的配置信息
  11. 基于樽海鞘群算法的线性规划求解matlab程序
  12. 3 步理清 Python 数据分析关键点,新手必看
  13. 页码在html中怎么设置,如何设置网页打印的网址、日期、页码等?
  14. netty pipeline 执行顺序解读
  15. 第四章css总结,第四章CSS层叠样式表分析.doc
  16. stm32f103电子钟心得体会_STM32中的时钟
  17. 以太网EMC接口电路设计及PCB设计
  18. 特斯拉第二季度电动汽车销量下降近 18%
  19. Android Q 适配指南 让你少走一堆弯路
  20. 2、nginx常用配置----作为web服务端

热门文章

  1. Kuerbernetes 1.11 二进制安装
  2. 基于SpringBoot vue的电脑商城平台源码和论文含支付宝沙箱支付
  3. 新5G网络架构较复杂 设立面对不少挑战
  4. 【echarts地图制作】下钻到乡镇/街道级别的
  5. SqlServer 触发器 详细讲解
  6. 江苏援沪“大白”们,用手绘漫画为上海加油
  7. Flutter国际化
  8. 分享一款 Google Pixel 2 独家动态壁纸
  9. 【SQL注入】SQL注入基本流程
  10. echarts的应用