完全理解顶峰APP开发Android系列中的RemoteViews

发布时间:   来源:文档文库   
字号:
完全理解顶峰APP开发Android系列中的RemoteViews
一、什么是RemoteViews
RemoteViews翻译过来就是远程视图.顾名思义,RemoteViews不是当前进程的View,是属于SystemServer进程.应用程序与RemoteViews之间依赖Binder实现了进程间通信.
二、RemoteViews的用法
RemoteViews使用最多的场合是通知栏和桌面小插件.以通知栏为例,讲解下它的用法.
1、新建一个Notification
这里要注意是在android3.0之前都是使用如下的形式构建一个Notification//1.新建一个Notification对象
NotificationmNotification=newNotification(;//2.添加属性,比如标题、内容、优先级、图片等mNotification.tickerText="这是通知栏的标题";mNotification.icon=R.drawable.ic_launcher;
mNotification.flags=Notification.FLAG_NO_CLEAR;
mNotification.setLatestEventInfo(this,"这是内容","这是标题",null;3.0之后官方推荐使用建造者模式创建Notification.
NotificationmNotification=newNotification.Builder(this
.setContentTitle("这是标题".setContentText("这是内容"
.setSmallIcon(R.drawable.ic_launcher.build(;
Notification有很多属性,这里列举一些-setContentTitle设置标题-setContentText设置内容
-setLargeIcon设置通知栏大图标-setSmallIcon设置通知栏小图标-setContent设置RemoteViews
-setContentIntent当通知条目被点击,就执行这个被设置的Intent.
-setDeleteIntent当用户点击"ClearAllNotifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行
-setLights设置闪光灯-setSound设置声音-setPriority设置优先级2、设置NotificationRemoteViews
如果要给通知栏使用自定义布局就要使用RemoteViews,传入包名和相应的布局.

RemoteViewsmRemoteViews=newR.layout.remoteview_layout;
RemoteViews("com.example.remoteviewdemo",
然后通过setContent(传入RemoteViews对象即可.
这里顺便讲一下PendingIntent,PendingIntent是”延迟意图”的意思,就是当满足某一条件时出触发这个Intent.通过PendingIntentgetActivitygetBroadcastgetService等分别构建一个打开对应组件的延迟Intent.
传入四个参数,contextintentrequestCode(自定义flag.
Intentintent=newIntent(MainActivity.this,MainActivity.class;PendingIntentmPendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent4flag.
-FLAG_ONE_SHOT只执行一次
-FLAG_NO_CREATE若描述的Intent不存在则返回NULL
-FLAG_CANCEL_CURRENT如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
-FLAG_UPDATE_CURRENT总是执行,这个flag用的最多3、获取通知管理者
NotificationManagermanager=(NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE;
4、弹出通知
调用notify方法,传入一个id(自定义和通知实例即可.
manager.notify(1,mNotification;5、例子
我用一个按钮弹出通知,点击这个通知时进入到该ActivitypublicclassMainActivityextendsActivity{
privateNotificationManagermanager;privateNotificationmNotification;
@Override
protectedvoidonCreate(BundlesavedInstanceState{super.onCreate(savedInstanceState;setContentView(R.layout.activity_main;//1.创建RemoteViews实例
RemoteViewsmRemoteViews=newRemoteViews("com.example.remoteviewdemo",

R.layout.remoteview_layout;
//2.构建一个打开ActivityPendingIntent
Intentintent=newIntent(MainActivity.this,MainActivity.class;
PendingIntentmPendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT;
//3.创建一个Notification
mNotification=newNotification.Builder(this.setSmallIcon(R.drawable.ic_launcher.setContentIntent(mPendingIntent.setContent(mRemoteViews.build(;
//4.获取NotificationManager
manager=(NotificationManagergetSystemService(Context.NOTIFICATION_SERVICE;
Buttonbutton1=(ButtonfindViewById(R.id.button1;button1.setOnClickListener(newOnClickListener({
@Override
publicvoidonClick(Viewv{
//弹出通知
manager.notify(1,mNotification;}};
}}
6、改变RemoteViews的布局
RemoteViews并不能直接获得控件实例,然后对控件进行操作.它提供了
setTextViewText(viewId,textsetImageViewResource(viewId,srcId等方法进行操作,传入控件id和相应的修改内容.列举一下常用的属性
-setTextViewText(viewId,text设置文本-setTextColor(viewId,color设置文本颜色-setTextViewTextSize(viewId,units,size设置文本大小-setImageViewBitmap(viewId,bitmap设置图片
-setImageViewResource(viewId,srcId根据图片资源设置图片-setViewPadding(viewId,left,top,right,bottom设置Padding间距-setOnClickPendingIntent(viewId,pendingIntent设置点击事件
我这里就以setTextViewText改变文本的属性来讲解改变RemoteViews的原理.

本文来源:https://www.2haoxitong.net/k/doc/6dca2d33a58da0116d1749cb.html

《完全理解顶峰APP开发Android系列中的RemoteViews.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式