RemoteViews的应用
- 通知栏
-
- res/layout/下建立小部件的布局文件
- res/xml/下建立小部件的配置信息
- 定义小部件的实现类(需要继承AppWidgetProvider类)
- 在AndroidManifest.xml中声明(因为其本质是一个BroadcastReceiver)
AppWidgetProvider中几个回调函数的说明
原型 说明 onEnabled 窗口小部件 第一次
添加到桌面时调用onUpdate 小部件第一次添加和每次更新时都会调用 onDeleted 每删除一次小部件会回调一次 onDisabled 最后一个该类型的小部件被删除时调用 onReceive 分发具体的事件给其他方法 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) 参数说明:
- requestCode: 表示发送方的请求码, 多数情况可以设置为0
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 RemoteViews的内部机制
RemoteViews支持的View类型
类型 具体 Layout FrameLayout, LinearLayout, RelativeLayout, GridLayout View AnalogClock, 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的更新过程
- RemoteViews通过Binder传递到SystemServer进程
- SystemServer根据RemoteViews中的包名信息获得应用的资源
- 系统执行通过RemoteViews的一系列set方法提交的更新操作
界面更新完成
- RemoteViews的set方法执行过程
- set方法对View的操作不是立即执行的
- 首先set后会提交一个ReflectionAction对象保存在RemoteViews的mAction成员中
- RemoteViews中的apply方法通过LayoutInflater加载RemoteViews的布局, 然后调用performApply执行更新操作
- performApply会依次调用mActions里面的Action对象的apply
RemoteViews的实际更新时机是在AppWidget#updateAppWidget和NofificationManager#notify方法时发生的
- RemoteViews的apply方法会加载布局而reapply只更新
- setOnClickPendingIntent只支持普通的View设置点击事件
- 如果要给ListView等item添加点击事件必须将setPendingIntentTemplate和setOnClickFillInParent组合使用才行
- RemoteViews的意义
5. Android Framework - RemoteViews
最新推荐文章于 2024-09-21 22:04:29 发布