三、RemoteViews
2015-10-18 16:54
148 查看
RemoteViews
RemoteViews在Android中的应用场景有两种:通知栏和桌面小部件。1.RemoteViews的应用
RemoteViews在实际的开发过程中,主要用在通知栏和桌面小部件的开发中。通知栏主要是通过NotificationManager的notify方法来实现的,它除了默认效果,还可以自定义布局。
桌面小部件则是通过AppWidgetProvider来实现的,它本质上是一个广播。
通知栏和桌面小部件的开发过程都会用到RemoteViews,它们在更新界面的时候无法像Activity里面那样去直接更新View,这是因为二者的界面都是运行在其他的进程中,确切来说是系统的SystemServer进程。
为了跨进程更新界面,RemoteViews提供了一系列的set方法,并且这些方法只是Views全部方法的子集,另外,RemoteViews中所支持的View类型也是有限制的。
(1)RemoteViews在通知栏的应用
1)系统默认样式的通知
sId++; 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, DemoActivity_2.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ); notification. setLatestEventInfo(this, "chapter_5", "this is notification.", pendingIntent ); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE ); manager.notify( sId, notification);
2)自定义样式的通知
sId++; 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, DemoActivity_1.class); intent.putExtra( "sid", "" + sId); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ); System. out.println( pendingIntent); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout. layout_notification ); remoteViews.setTextViewText(R.id. msg, "chapter_5: " + sId); remoteViews.setImageViewResource(R.id. icon, R.drawable.icon1); PendingIntent openActivity2PendingIntent = PendingIntent . getActivity(this, 0, new Intent( this, DemoActivity_2.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( sId, notification);
更新RemoteViews必须通过RemoteViews所提供的一系列方法来更新view。
(2)RemoteViews在桌面小部件的应用
1)定义小部件
在res/layout下面新建一个XML文件,命名为widget.xml,名称和内容可以
自定义,看这个小部件要做成什么样子。
" data-snippet-id="ext.5063da03450d04796e7b64ff3e42b6bc" data-snippet-saved="false" data-codota-status="done">[code]<?xml version="1.0" encoding= "utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation="vertical" > <ImageView android:id= "@+id/imageView1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/icon1" /> </LinearLayout>
2)定义小部件配置信息
在res/xml下新建appwidget_provider_info.xml,名称随意,添加内容:
" data-snippet-id="ext.b20724e5001ce1fbd06589a7a74fd134" data-snippet-saved="false" data-codota-status="done">[code]<?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>
上面的参数的意思:
initialLayout是小工具初始化的布局;
minHeight和minWidth定义小工具的最小尺寸;
updatePeriodMillis定义小工具的自动更新的周期,毫秒为单位。
3)定义小部件的实现类
4)Manifest声明
<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.ryg.chapter_5.action.CLICK" /> <action android:name= "android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter > </receiver >
两个action,第一个用于标识识别小部件的单机行为,第二个则作为小部件的标识,必须存在。
未完。。。
这篇估计不会写了
:cry:
相关文章推荐
- 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)
- Linux软连接和硬链接
- 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)
- 问世间最大的乐趣是什么?
- JS编程去掉注释部分
- 51nod 1423:最大二“货”
- PIC单片机ISP下载外围链接电路
- chapter 12
- 条款34:区分接口继承以及实现继承
- 51nod 1423:最大二“货”
- windows安装TortoiseGit详细使用教程
- JDBC 基本操作案例
- HTML学习笔记之超链接的使用
- ios UITableView 异步加载图片并防止错位
- 基于ZooKeeper的分布式锁和队列
- Oracle中函数、存储过程、触发器
- 对象之间的调用机制 通信方式
- 〈灰色系统理论及其应用〉第7版,刘思峰 P153例7.3.1
- phpredis:php一个key-value扩展
- 九度OJ 1036:Old Bill (老比尔) (基础题)