RemoteView应该是一种远程View,表示的是一个View结构,他可以在其它进程中显示.

    在android中使用场景有两种:通知栏和桌面小部件

    5.1 RemoteView的应用

        5.1.1 RemoteView在通知栏上的应用

          使用系统默认的样式弹出一个通知

              

Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello world";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatesEventInfo(this, "hello", "this is notification.", pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);

          使用RemoteViews

Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello world";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
remoteViews.setTextView(R.id.msg, "hello");
remoteViews.setImageViewResource(R.id.icon, R.drawable,icon1);
PendingIntent openActivity2PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Activity.class), PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2PendingIntent);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, notification);

          需要提供包名和布局文件的资源id即可创建一个RemoteViews对象.

    5.1.2 RemoteViews在桌面小部件上的应用

        AppWidgetProvider是android中提供的用于实现桌面小部件的类,本质上是一个广播,即BroadcastReceiver.

      1. 定义小部件界面(在res/layout/下新建一个XML文件例:widget.xml)
      2. 定义小部件配置信息(在res/xml/下新建appwidget_provider_info.xml)

              <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

                  android:initialLayout="@layout/widget"//初始化布局

                  android:minHeight="84dp"      //最小尺寸

                  android:minWidth="84dp"

                  android:updatePeriodMillis="8640000">//自动更新的周期

              </appwidget-provider>

          3.定义小部件的实现类(需要继承AppwidgetProvider)

          4.在AndroidManifest.xml中声明小部件

              <receiver

                android:name=".MyAppWidgetProvider">

                <meta-date

                  android:name="android:appwidget.provider"

                  android:resource="@xml/appwidget_provider_info">

                </meta-data>

                <intent-filter>

                  <action android:name="com.example.action.CLICK"/>//用于识别小部件的单击行为

                  <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>//这个Action座位小部件的标志而必须存在,如果不加,这个receiver就不是一个桌面小部件而且也无法出现在手机小部件列表中

                </intent-filter>

              </receiver>

    5.1.3 PendingIntent概述

        一种处于pending状态的意图,而pending状态表示的是一种待定,等待,即将发生的意思.

        PendingIntent是在将来某个不确定的时刻发生,而Intent是立刻发生的.

        PendingIntent支持三种待定意图:启动Activity,启动Service和发送广播.

        PendingIntent的匹配规则:如果两个PendingIntent他们内部的Intent相同并且requestCode也相同,那么这两个PendingIntent就是相同的.

    5.2 RemoteViews的内部机制

        RemoteViews的作用是在其他进程中显示并更新View界面.

        最常用的构造方法:public ReoteViews(String packageName, int layoutId);

        RemoteViews没有提供findViewById方法,因此无法直接访问里面的View元素,而必须通过RemoteViews所提供的一些列set方法.

        RemoteViews的内部机制图:

          

 

        

第五章:理解RemoteViews的更多相关文章

  1. Android开发艺术探索第五章——理解RemoteViews

    Android开发艺术探索第五章--理解RemoteViews 这门课的重心在于RemoteViews,RemoteViews可以理解为一种远程的View,其实他和远程的Service是一样的,Rem ...

  2. Android开发艺术探索》读书笔记 (5) 第5章 理解RemoteViews

    第5章 理解RemoteViews 5.1 RemoteViews的应用 (1)RemoteViews表示的是一个view结构,它可以在其他进程中显示.由于它在其他进程中显示,为了能够更新它的界面,R ...

  3. Pro Android 4 第五章 理解Intent

         Android引入了一个名为Intent的概念用来唤醒各种组件.Android中的组件包括:activities(UI 组件),services(后台代码),broadcast receiv ...

  4. 《Linux命令行与shell脚本编程大全》 第五章理解shell

    5.1 1. cat /etc/passwd 可以查看每个用户自己的默认的shell程序. 2.默认的交互shell会在用户登录某个虚拟控制台终端时启动. 不过还有另外一个默认的shell是/bin/ ...

  5. 深入理解 C 指针阅读笔记 -- 第五章

    Chapter5.h #ifndef __CHAPTER_5_ #define __CHAPTER_5_ /*<深入理解C指针>学习笔记 -- 第五章*/ /*不应该改动的字符串就应该用 ...

  6. 《深入理解java虚拟机》读书笔记四——第五章

    第五章 调优案例分析与实战

  7. Nova PhoneGap框架 第二章 理解index.html

    跟绝大多数PhoneGap程序一样,Index.html是程序的入口.这个页面应该完成应用程序的初始化工作. 首先,让我们来看看这个页面通常都长什么样子: 下面我将一一解释这个页面都做了哪些初始化工作 ...

  8. 《Entity Framework 6 Recipes》中文翻译系列 (22) -----第五章 加载实体和导航属性之延迟加载

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第五章 加载实体和导航属性 实体框架提供了非常棒的建模环境,它允许开发人员可视化地使 ...

  9. 精通Web Analytics 2.0 (7) 第五章:荣耀之钥:度量成功

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第五章:荣耀之钥:度量成功 我们的分析师常常得不到我们应得的喜欢,尊重和资金,因为我们没有充分地衡量一个黄金概念:成果.因为我们 ...

随机推荐

  1. Day7-微信小程序实战-引入iconfont(充分利用iconfont图标库的资源)

    一.引入iconfont 首先在iconfont.com中注册登陆: 点击上方[图标管理]并进入我的项目 注意:如果没有项目的话,就点击右边的来创建项目 在官网中找到想要的图标之后,以SVG的形式下载 ...

  2. xenomai内核解析之双核系统调用(一)

    版权声明:本文为本文为博主原创文章,转载请注明出处.如有错误,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 目录 xenomai 内核系统调用 一.32位Lin ...

  3. Dubbo面试专题

    Dubbo面试专题 1. 什么是dubbo Dubbo是阿里巴巴SOA服务化治理方案的核心框架,是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 2.  ...

  4. spring boot 配置虚拟静态资源文件

    我们实现的目的是:通过spring boot 配置静态资源访问的虚拟路径,可实现在服务器,或者在本地通过:http://ip地址:端口/资源路径/文件名  ,可直接访问文件 比如:我们本地电脑的:E: ...

  5. Tensorflow实现神经网络的前向传播

    我们构想有一个神经网络,输入为两个input,中间有一个hidden layer,这个hiddenlayer当中有三个神经元,最后有一个output. 图例如下: 在实现这个神经网络的前向传播之前,我 ...

  6. [ C++ ] 勿在浮沙筑高台 —— 内存管理(18~31p) std::alloc

    部分内容个人感觉不是特别重要,所以没有记录了.其实还是懒 embedded pointers 把对象的前四字节当指针用. struct obj{ struct obj *free_list_link; ...

  7. 版本控制工具 GIT入门教程

    GIT 在团队中的中作流程 1.每个程序员在自己的分支上进行开发 2.主程序猿/Leader合并程序员程序 3.程序员之间也可以对一下提交冲突进行合并 下载和安装 GIT官方网址:http:// gi ...

  8. swagger ui demo

    前言 前几天一个朋友公司在用Springboot集合swagger时候总是从浏览器看不了接口,我两找了问题,但是他还是没有找到,于是我就自己从http://start.spring.io/上下载了一个 ...

  9. Springboot 集成 ElasticSearch 踩坑

    这里只涉及到基础使用 导包 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  10. (1)RabbitMQ简介与安装

    1.RabbitMQ简介 因为RabbitMQ是基于开源的AMQP协议来实现的,所以在了解MQ时候,首先我们来了解下AMQP协议.AMQP,即Advanced Message Queuing Prot ...