5. Android Framework - RemoteViews

  1. RemoteViews的应用

    1. 通知栏
    2. 桌面小部件

      • res/layout/下建立小部件的布局文件
      • res/xml/下建立小部件的配置信息
      • 定义小部件的实现类(需要继承AppWidgetProvider类)
      • 在AndroidManifest.xml中声明(因为其本质是一个BroadcastReceiver)

      AppWidgetProvider中几个回调函数的说明

    原型说明
    onEnabled窗口小部件第一次添加到桌面时调用
    onUpdate小部件第一次添加和每次更新时都会调用
    onDeleted每删除一次小部件会回调一次
    onDisabled最后一个该类型的小部件被删除时调用
    onReceive分发具体的事件给其他方法
    1. PendingIntent概述
      表示一个在接下来某个特定时刻发生的Intent
      PendingIntent支持三种特定Intent: 启动Activity, 启动Service和发送广播

      PendingIntent的主要方法

    原型说明
    getActivity(Context cxt, int requestCode, Intent i, int flags)相当于Context.startActivity(Intent)
    getService(Context cxt, int requestCode, Intent i, int flags)相当于Context.startService(Intent)
    getBroadcast(Context cxt, int requestCode, Intent i, int flags)相当于Context.sendBroadcast(Intent)

    参数说明:

    1. requestCode: 表示发送方的请求码, 多数情况可以设置为0
    2. flags: 对当前PendingIntent的影响

      PendingIntent的匹配规则: 如果两个PendingIntent的requestCode和其内部的Intent相同, 那么这两个PendingIntent就是相同的. 内部Intent相同指的是两个Intent的ComponentName和intent-filter相同

      PendingIntent的flags

    flag说明
    FLAG_ONE_SHORT当前描述的PendingIntent只能使用一次, 然后自动cancel
    FLAG_NO_CREATE当前描述的PendingIntent不会主动创建, 很少用
    FLAG_CANCEL_CURRENT如果当前描述的PendingIntent已存在, 它们都会被cancel, 然后系统创建新的PendingIntent
    FLAG_UPDATE_CURRENT如果当前描述的PendingIntent已存在, 那么更新当前的PendingIntent
  2. RemoteViews的内部机制

    RemoteViews支持的View类型

    类型具体
    LayoutFrameLayout, LinearLayout, RelativeLayout, GridLayout
    ViewAnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper, ViewStub

    访问RemoteViews里面的View需要使用以下方法

    原型作用
    setTextViewText(int viewId, CharSequence text)设置TextView的文本
    setTextViewTextSize(int viewId, int units, float size)设置TextView的字体大小
    setTextColor(int viewId, int color设置TextView的字体颜色
    setImageViewResource(int viewId, int srcId)设置ImageView的图片资源
    setImageViewBitmap(int viewId, Bitmap bitmap)设置ImageView的图片
    setInt(int viewId, String methodName, int value)反射调用View对象的参数类型为int的方法
    setLong(int viewId, String methodName, long value)反射调用View对象的参数类型为long的方法
    setBoolean(int viewId, String methodName, boolean value)反射调用View对象的参数类型为boolean的方法
    setOnClickPendingIntent(int viewId, PendingIntent pendingIntent为View添加单击事件, 事件类型只能是PendingIntent
    • RemoteViews的更新过程

      1. RemoteViews通过Binder传递到SystemServer进程
      2. SystemServer根据RemoteViews中的包名信息获得应用的资源
      3. 系统执行通过RemoteViews的一系列set方法提交的更新操作
      4. 界面更新完成

        • RemoteViews的set方法执行过程
      5. set方法对View的操作不是立即执行的
      6. 首先set后会提交一个ReflectionAction对象保存在RemoteViews的mAction成员中
      7. RemoteViews中的apply方法通过LayoutInflater加载RemoteViews的布局, 然后调用performApply执行更新操作
      8. performApply会依次调用mActions里面的Action对象的apply
      9. RemoteViews的实际更新时机是在AppWidget#updateAppWidget和NofificationManager#notify方法时发生的

        • RemoteViews的apply方法会加载布局而reapply只更新
        • setOnClickPendingIntent只支持普通的View设置点击事件
        • 如果要给ListView等item添加点击事件必须将setPendingIntentTemplate和setOnClickFillInParent组合使用才行
  3. RemoteViews的意义
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值