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

  • 在通知栏上的作用
    • 普通的通知栏
    • 自定义通知栏
Intent intent = new Intent(this,MainActivity.class);        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);                Notification notification = new Notification.Builder(this)                        .setTicker("hello world")                        .setSmallIcon(R.mipmap.ic_launcher)                        .setWhen(System.currentTimeMillis())                        .setContentTitle("normal_notifycation")                        .setContentText("this is normal_notifycation")                        .setContentIntent(pendingIntent)                        .setAutoCancel(true)                        .build();                manager.notify(1,notification);

普通的通知栏很简单,可以看到,Notification也是一种建造者模式。这个以后会说到。

那么来说下remoteviews自定义通知栏用法。

Notification notification1 = new Notification();                notification1.icon = R.mipmap.ic_launcher;                notification1.tickerText = "remoteview";                notification1.when = System.currentTimeMillis();                notification1.flags = Notification.FLAG_AUTO_CANCEL;                RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.layout_notifycation);                remoteViews.setTextViewText(R.id.msg, "remoteviews text");                remoteViews.setTextViewText(R.id.open_activity2, "click me");                remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);                PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);                remoteViews.setOnClickPendingIntent(R.id.open_activity2, pendingIntent1);                notification1.contentView = remoteViews;                notification1.contentIntent = pendingIntent1;                manager.notify(2,notification1);

大致代码就是这样,不过在这里我要说明几点:

  1. 在5.1手机上,上面代码是不会显示文字的,为什么呢,因为默认的字体颜色是白色,而通知栏背景是白色,这样就导致看不见了。所以我们需要自己根据需求去设置;
  2. romoteviews提供了set….的方法,根据需求去设置
  • RemoteView在桌面小部件上的应用

使用步奏:

  1. 定义小部件界面
    在res/layout下面新建xml布局文件
  2. 定义小部件配置信息
    在res/xml中新建xml文件,用来配置
<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="84dp" android:minWidth="84dp" android:updatePeriodMillis="86400000" ></appwidget-provider>

有哪些属性可用呢,看下图,属性的含义就不具体说了。图上很明白

3. 定义小部件实现类
实现类要继承AppWidgetProvider类,这个类就是BroadcastReceiver的子类。这个如下

方法名一步了然,就不多少了,需要的去看任玉刚
4. 在manifast中配置

<receiver android:name=".MyAppWidgetProvider">            <meta-data  android:name="android.appwidget.provider" android:resource="@xml/appwidget_provider_info">            </meta-data>            <intent-filter>                <action android:name="com.gl.action.CLICK"/>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>            </intent-filter>        </receiver>

按上面代码的格式配置就好。这里给出郭神的一个链接小火箭传送门

  • PendingIntent
    PendingIntent表示一种意图,不是立刻发生,是在将来某个时候发生的Intent。
    • getActivity
    • getService
    • getBroadcast
      通过上面3中方法来获取对应的PendingIntent。

PendingIntent的匹配规则:
如果两个PendingIntent他们内部的Intent相同并且requestCode也相同,那么这两个PendingIntent就是相同的。Intent相同的规则是:ComponentName和intent-filter都相同。
关于4种flag,看书或者文档。

  • RemoteViews的内部机制
    关于源码分析,我就不敢羞人现眼了。

更多相关文章

  1. Android(安卓)NotificationManager简读
  2. Android简单实现仿微信选择图片以及拍照(PhotoPicker)
  3. Jenkins奇技淫巧 — 环境配置篇(Android自动化构建)
  4. Android(安卓)Studio 关联源码配置方法
  5. Android(安卓)Studio配置javah自动生成头文件
  6. Mac上配置gradle遇到的问题
  7. Android系统启动之配置文件解析
  8. 配置dialog无标题的几种方法
  9. Android(安卓)Gradle Plugin指南(二)——基本项目

随机推荐

  1. Android移动应用界面的模板化设计
  2. Android(安卓)横向ScrollView照片浏览器
  3. Android(安卓)高级UI
  4. [置顶] android 新浪微博客户端的表情功
  5. Android(安卓)正移植到C#
  6. Android方法数
  7. AndroidStudio Build过程解析
  8. Android(安卓)企业需求与开发者状况简析(
  9. 为了获取下一个10亿用户,Google的布局其实
  10. Android:规范命名,让合作更加愉快