理解RemoteViews

什么是remoteViews?按照字面意思是远程View,但RemoteViews并没有继承View,而是继承Object。它的作用是可以跨进程更新界面,听起来有点神奇。RemoteViews在Android中的使用场景有两种:通知栏和桌面小部件

RemoteViews的应用

1.在通知栏上的应用

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

Notification notification= new Notification();

notification.icon = R.drawable.notify;

notification.when = System.currentTimeMillis();

notification.tickerText = "通知";

notification.flags = Notification.FLAG_AUTO_CANCEL;

//创建延时意图

Intent intent = new Intent(this, OtherActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(this,"通知", "具体内容", pendingIntent):

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

nm.notify(0, notification);

②使用RemoteViews可以自定义布局,通过RemoteViews来加载布局文件即可改变通知的样式,具体代码如下

Notification notification= new Notification();

notification.icon = R.drawable.notify;

notification.when = System.currentTimeMillis();

notification.tickerText = "通知";

notification.flags = Notification.FLAG_AUTO_CANCEL;

//创建延时意图

Intent intent = new Intent(this, OtherActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);

remoteViews.setTextViewText(R.id.msg, "自定义通知");

remoteViews.setImageViewResource(R.id.icon, R.drawable.icon);

PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,

new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.btn, pendingIntent2);

notification.contentView = remoteViews;

notification.contentIntent = pendingIntent;

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

nm.notify(1, notification);

2.在桌面小部件上的应用

AppWidgetProvider是Android中提供的用于实现桌面小部件的类,实际上是一个广播。具体实现步骤如下

①定义小部件界面

自定义xml布局文件,具体布局样式根据实际开发需要,如widget.xml

②定义小部件配置信息

在res/xml下新建appwidget_provider_info.xml,名称任意

android:initialLayout="@layout/widget"

android:minHeght="84dp"

android:minWidth="84dp"

android:updatePeriodMillis="86400000" >

③定义小部件的实现类

public class MyAppWidgetProvider extends AppWidgetProvider {

private static final String CLICK_ACTION = "com.hj.action.click";

@Override

public void onReceive(final Context context, Intent intent) {

super.onReceive(context, intent);

if (intent.getAction().equals(CLICK_ACTION)) {

new Thread(new Runnable() {

@Override

public void run() {

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),

R.drawable.ic_launcher_v2);

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

for (int i = 0; i < 10; i++) {

float degree = (i * 10) % 360;

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

remoteViews.setImageViewBitmap(R.id.container, rotateBitmap(context, bitmap, degree));

Intent intent1 = new Intent(CLICK_ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);

remoteViews.setOnClickPendingIntent(R.id.container, pendingIntent);

appWidgetManager.updateAppWidget(new ComponentName(context, MyAppWidgetProvider.class), remoteViews);

SystemClock.sleep(30);

}

}

}).start();

}

}

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

super.onUpdate(context, appWidgetManager, appWidgetIds);

int count = appWidgetIds.length; for (int i = 0; i < count; i++) {

int appWidgetId = appWidgetIds[i];

onWidgetUpdate(context, appWidgetManager, appWidgetId);

}

}

private void onWidgetUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

Intent intent = new Intent();

intent.setAction(CLICK_ACTION);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

remoteViews.setOnClickPendingIntent(R.id.container, pendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

}

private Bitmap rotateBitmap(Context context, Bitmap bitmap, float degree) {

Matrix matrix = new Matrix();

matrix.reset();

matrix.setRotate(degree);

Bitmap tmpBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),

bitmap.getHeight(), matrix, true);

return tmpBitmap;

}

}

④在AndroidManifest.xml声明小部件

桌面部件实际上是一个广播组件,必须要注册。APPWIDGET_UPDATE代表该广播是作为小部件的标识存在的

android:name=".MyAppWidgetProvider>

android:name="android.appwidget.provider"

android:resource="@xml/appwidget_provider_info">

PendingIntent

PendingIntent表示一种处于pending状态的意图,而pending状态表示的是一种待定、等待、即将发生的意思。PendingIntent支持三种待定意图:启动Activity、启动Service和发送广播。

1.PendingIntent的Flag介绍

FLAG_ONE_SHOT

PendingIntent只能被使用一次,然后会自动cancel;如果后续还有相同的PendingIntent,那么他们的send方法就会调用失败

FLAG_NO_CREATE

PendingIntent不会主动创建,如果当前PendingIntent不存在,那么三种意图方法调用会直接返回null,获取PendingIntent会失败,它无法单独使用

FLAG_CANCEL_CURRENT

PendingIntent如果已经存在,那么它们都会被cancle,然后系统会创建一个新的PendingIntent。对于通知栏而言,那些被cancel的消息单击后无法打开

FLAG_UPDATE_CURRENT

PendingIntent如果已经存在,那么它们都会被更新

通知栏而言,notify(int, notification)方法中,若id值每次都不同的话,需要考虑到flag参数对应消息接收的情况

RemoteViews内部机制

RemoteViews目前并不能支持所有的View的类型,不能支持自定义View、EditText等,同时没有提供findViewById方法,无法直接访问View元素。RemoteViews会通过Binder传递到SystemServer进程,系统会通过RemoteViews中包名等信息去得到该应用的资源并加载布局文件,当需要更新RemoteViews时,我们需要通过一系列set方法并通过NotificationManager和AppWidgetManager来提交更新任务,具体的更新操作也是在SystemServer进程中完成的。RemoteViews提供一个Action的概念,Action代表一个View操作,系统首先将View操作封装到Action对象并跨进程传输到远程进程,接着在远程进程中执行Action对象中的具体操作

android remoteviews 设置背景,理解RemoteViews相关推荐

  1. Android Activity 设置背景图片

    设置Activity图片背景 Android(Activity)设置背景图片方法: xml布局中用andriod:background = "@drawable/bgimage"或 ...

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

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

  3. Android如何设置背景图片

    一.通过XML文件(优先级最高,不会影响到其他activity) 在activity的xml配置文件中添加 android:background="@drawable/bg",bg ...

  4. android动态设置背景

    最近,实现了这么一个功能,本来是将用户的登录注册.退登注销等做成了sdk,便于模块化复用. 这中间涉及到UI,公司多个项目最好是统一这方面的UI,才能实现完全复用.当然,一些小改动还是必要的,如设置状 ...

  5. 如何在android中设置背景图片,在Android中设置窗口背景图

    Android窗口管理 在整个控件树的最顶端,是一个逻辑的树顶,ViewParent,在源码中的实现是ViewRoot(ViewRoot extends Handler implements View ...

  6. android toast设置背景颜色,Android 彩色Toast的实现代码

    Android默认的Toast太丑了,我们来封装一个花里胡哨的Toast吧,就叫ColoredToast. 效果: Toast有一个setView方法,通过它我们可以设置自定义的布局,这里我只是加入了 ...

  7. android ImageButton设置背景图片无法显示

    android的Button属性有一套默认的长宽大小,直接使用background给button添加背景图片,此时如果图片远大于所需的大小就会出现无法显示的情况.我的解决办法是将ImageButton ...

  8. android 布局设置背景的透明度

    半透明<Button android:background="#e0000000" ... /> 透明<Button android:background=&qu ...

  9. android dialog设置背景图片,如何为Dialog设置背景图片?

    在资源编辑器中打开'.res'文件,然后选择您喜欢的主题, 在"未选中"选项卡下打开DialogContentPane样式,如果您还没有创建它,请查看本答案的结尾,如何操作?并将背 ...

最新文章

  1. 一文读懂神经网络初始化!吴恩达Deeplearning.ai最新干货
  2. JSP学习笔记04-request
  3. ios找不到信任证书_ios信任苹果企业级应用
  4. 天翼云从业认证(3.6)了解天翼云大数据SaaS服务
  5. 【Quartz】问题记录注意事项【四】
  6. astype函数_从Excel到Python:最常用的36个Pandas函数!最完整的Pandas教程!
  7. php标准输出重定向,python标准输出重定向方式
  8. 患者数据库mysql_关系型数据库之MySQL基础总结_part1
  9. 湖北职业技术学院计算机协会,湖北职业技术学院2019年教师教学能力大赛顺利举行...
  10. Python手册(Python Basics)--Python基础
  11. 解决Linux下redis客户端工具连接不到redis服务
  12. 【信号与系统】z变换
  13. 英文写作第一反应词替换表
  14. 图片转文字怎么转换?分享你个简单的方法
  15. mac pro 键帽 方向键 上下键 拆卸
  16. Java SE 8 Archive Downloads (JDK 8u202 and earlier)
  17. SCL编程指南,常用样例
  18. 【RL系列】Multi-Armed Bandit问题笔记
  19. 基于php食堂外卖系统
  20. windows 操作系统下载

热门文章

  1. CF #768 F.Flipping Range
  2. html 前端优化上传视频,前端上传组件Plupload使用---上传大视频(分片上传)
  3. 4.计算机网络与信息安全
  4. Yolov3没框原因和解决办法
  5. 代码质量保证体系(下)
  6. Linux:shell编程(shell基本语法)
  7. 学java去中公还是黑马_公务员考试复习:巧做一匹大黑马
  8. webrtc 之 sip trickle ice
  9. JAVA中String、StringBuffer和StringBuider类
  10. 计算机网络 交换机工作原理