RemoteViews所支持的View类型如下图所示(注意不支持下图中View的子类):

RemoteViews所支持的View的类型

RemoteViews没有提供findViewById方法,因为RemoteViews在远程进程中显示,因此无法直接访问里面的View元素,而必须通过RemoteViews所提供的一系列set方法来完成,部分set方法如下所示:

1:在Notification中的应用

示例代码如下:

    private Button button;private Notification notification;private NotificationManager notificationManager;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.ceshi);button = findViewById(R.id.button);Intent intent=new Intent(this, com.example.liang.arlvyou.lanjiazai.ar.MainActivity.class);Intent remote=new Intent(this, RemoteActivity.class);PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);PendingIntent remoteIntent=PendingIntent.getActivity(this,0,remote,PendingIntent.FLAG_UPDATE_CURRENT);RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.remoteview);remoteViews.setTextViewText(R.id.textView2,"我是remoteview");remoteViews.setImageViewResource(R.id.image,R.drawable.icon);remoteViews.setOnClickPendingIntent(R.id.button2,remoteIntent);notification=new Notification();//注意Notification创建得用new的方式了创建,因为//如果用Builder的方式创建的话,在设置remoteview的时候会用到setCustomContentView方法,//该方法需要api24才能使用,api版本太高了,但是使用new的方式就可以不用考虑低api的问题。notification.icon=R.mipmap.ic_launcher;notification.tickerText="hello,world";notification.when=System.currentTimeMillis();notification.contentView=remoteViews;notification.contentIntent=pendingIntent;notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {notificationManager.notify(1,notification);}});}

remoteview文件中的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="TextView"android:layout_width="100sp"android:layout_height="wrap_content"android:id="@+id/textView2"/><ImageViewandroid:layout_width="100sp"android:layout_height="wrap_content"android:id="@+id/image"/><Buttonandroid:text="Button"android:layout_width="100sp"android:layout_height="wrap_content"android:id="@+id/button2"/>
</LinearLayout>

2:在桌面小组件中的应用,直接运行应用就行,都不需要打开应用,桌面小组件就已经有了。

示例代码如下:

public class MyAppWidgetProvider extends AppWidgetProvider {@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {Log.d("MyAppWidgetProvider", "调用了onUpdate方法");Intent intent = new Intent(context, com.example.liang.arlvyou.lanjiazai.ar.MainActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteview);remoteViews.setImageViewResource(R.id.image, R.drawable.icon);remoteViews.setTextViewText(R.id.textView2, "我是桌面小组件");remoteViews.setOnClickPendingIntent(R.id.textView2, pendingIntent);appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);//用updateAppWidget来更新remoteViews的状态}
}

在androidmanifest.xml文件中

...<receiver android:name=".lanjiazai.MyAppWidgetProvider"><meta-dataandroid:name="android.appwidget.provider" <!--name的值是固定的,只能是android.appwidget.provider-->android:resource="@xml/widget"/><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE"/><!--必须得有这个action--></intent-filter></receiver>
...

在res的xml文件夹下创建桌面组件的信息文件,比如widget.xml,文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:initialLayout="@layout/remoteview"android:minWidth="100dp"android:minHeight="100dp"android:updatePeriodMillis="86400000">
</appwidget-provider>

作者:名字_都被占了
链接:https://www.jianshu.com/p/6b31812ed5fa
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

RemoteViews的用法相关推荐

  1. 完全理解Android中的RemoteViews

    一.什么是RemoteViews RemoteViews翻译过来就是远程视图.顾名思义,RemoteViews不是当前进程的View,是属于SystemServer进程.应用程序与RemoteView ...

  2. android RemoteViews用法

    RemoteViews类描述了一个View对象能够显示在其他进程中,可以融合从一个 layout资源文件实现布局.虽然该类在android.widget.RemoteViews而不是appWidget ...

  3. Android关于notification的在不同API下的用法说明

    当我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们 ...

  4. Autofill Framework(自动填写)用法详解

    文/arjinmc 本文是基于官方demo来分析Autofill Framework的用法(要正常打开这个项目请使用Android Studio Preview 3.0以上版本,并下载Anroid O ...

  5. 【安卓】——Autofill Framework(自动填写)用法详解

    本文是基于官方demo来分析Autofill Framework的用法(要正常打开这个项目请使用Android Studio Preview 3.0以上版本,并下载Anroid O模拟器镜像).Aut ...

  6. RemoteViews讲解

    RemoteViews在日常开发中主要用于通知栏和桌面小部件的开发.它继承了Parcelable,所以可以很好的通过Binder来进行跨进程通信,里面提供了一系列的方法用于跨进程的更新界面.Remot ...

  7. android RemoteViews解析

    1.RemoteViews的作用 在其他进程中显示并更新view界面,所谓跨进程是因为view界面是运行在系统的SystemServer进程的.系统除了常见的notification和appwidge ...

  8. Android之Notification的多种用法

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51074832   文章出自:[薛瑄的博客](http://blog.csdn.ne ...

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

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

最新文章

  1. 论文《一种金融市场预测的深度学习模型:FEPA》(3)--EMD+PCA
  2. Zend Studio 10代码格式化设置
  3. 电脑打字学习_VOL.3,NO.2 | 小学一年级,爸爸管学习,完全就是个笑话!
  4. shsh验证服务器,教你从Cydia上取出SHSH并验证有效性!
  5. mac php 超时,PHP---Mac上开启php错误提示
  6. c语言单元二实验报告,C语言第七次实验报告
  7. 微擎写Android接口json,【微擎教程】getLocation需要在app.json中声明permission字段
  8. PHP新手之学习类与对象(4)
  9. 库,表,记录的相关操作
  10. Search Engine -垂直搜索小汇总
  11. 浪曦视频--工厂方法模式
  12. IE9打开的html文件打印不了,IE9无法查看打印预览的2个解决方法
  13. 如何提高数学分析水平(转载)
  14. iMeta | 北大陈峰/陈智滨等发表口腔微生物组研究中各部位取样的实验方法(Protocol)...
  15. Simpson’s Rule (辛普森法则)
  16. 国科大学习资料--最优化计算方法(王晓)--期末考试试卷2
  17. System Generator从入门到放弃(十)-ADC应用之音频信号采集与输出
  18. python的mathceil_Python ceil() 函数
  19. 北理工在线作业计算机的主要特点是( ),18春北理工《用户界面设计》在线作业-2...
  20. Linux 部署安装禅道教程

热门文章

  1. Android11 热点配置信息保存分析
  2. 用html做完整网页效果
  3. 光伏项目电力监控系统的重要
  4. burp suite抓包中文乱码
  5. 20189220 余超《Linux内核原理与分析》第二周作业
  6. php安装失败,phpcms安装失败怎么办
  7. Linux下安装bugzilla
  8. mysql无法连接的sha2加密问题
  9. 2021年国际及中国钢材价格走势、价格变化因素及后期钢材价格走势分析[图]
  10. 荣联云发送短信验证码--python3接口