android的remoteView通常用于更新远程界面。 它实现了Paracelable接口,可以通过Bindler传输到远程。 还有这个班的
APP会更新界面。 其实,当我们用这种特殊的方法更新接口时
该类将操作封装为Action对象,存储在内部集合中,然后管理类
提交操作,并在远程view的应用程序中遍历并执行操作集合
更新操作。
android中的remoteView添加点击操作是通过延迟意图PentingIntent来执行的。
应用场景:通知栏和桌面部件,更新不同进程之间的接口
桌面部件和通知栏分别由AppWidgetManager和通知管理器管理。
分别与系统服务进程的AppWidgetServer和通告程序进行通信
因此,更新接口需要RemoteView。 RemoteView实现了Paracelable,通过Bindler
系统服务进程中。
通知栏:
通知通知=new通知(;
notification.icon=r.drawable.IC _ launcher;
notification.ticker text=' hello world ';
notification.when=system.current time millis (;
notification.flags=notification.flag _ auto _ cancel;
intentintent=newintent(this,NotificationActivity.class );
//定义延迟意图
pendingintentpendingintent=pending intent.get activity (
0,intent,pending intent.flag _ cancel _ current;
notification.setlatesteventinfo (this,'我的标题','我的文本',pendingIntent );
remoteviewsremoteviews=newremoteviews (get package name )、
R.layout.remoteview;
remote views.settextviewtext (r.id.title,'我的标题');
remote views.settextviewtext (r.id.content,“我的内容”);
notification.content view=remote views;
notification.content intent=pending intent;
通知管理器=(通知管理器) getsystemservice (context.notification _ service );
manager.notify(1,通知);
桌面部件的实现:
1自定义布局
2定义配置文件
3自定义类继承AppWidgetProvider
4功能列表注册
定义配置文件:
功能列表配置:
自定义类:
publicclassmyappwidgetproviderextendsappwidgetprovider {
@Override
公共语音接受(finalcontextcontext,Intent intent ) )。
//TODO自动生成的方法存根
super.onreceive(context,intent );
/**将图像旋转一周*/
新标题(新运行) )。
@Override
公共语音运行(}
bitmap bitmap=bitmap factory.decode resource (context.get resources )、
R.drawable.ic_launcher;
appwidgetmanagermanager=appwidgetmanager.getinstance (context )
for(intI=0; i37; I ) {
浮动深度=(I * 10 ) 60;
remoteviewsremoteviews=newremoteviews (context.get package name )、
r.layout .构件;
remote views.setimageviewbitmap (r.id.imageview,rotatebitmap )上下文、bitmap和degree );
componentnamecn=newcomponentname (context,MyAppWidgetProvider.class );
//更新接口
manager.updateappwidget(cn,remoteViews;
}
}
().start );
}
@Override
public void on update (上下文上下文,appwidgetmanagerappwidgetmanager,
int[] appWidgetIds ) {
//TODO自动生成的方法存根
super.onupdate(context,appWidgetManager,appWidgetIds );
}
/**添加第一个实例时*/
@Override
publicvoidonenabled (上下文) {
//TODO自动生成的方法存根
super.onenabled(context );
}
/**删除实例时调用*/
@Override
publicvoidondeleted (上下文上下文,int[] appWidgetIds ) {
//TODO自动生成的方法存根
super.ondeleted(context,appWidgetIds;
}
/**删除最后一个实例时*/
@Override
公共上下文(context context ) {
//TODO自动生成的方法存根
super.ondisabled(context;
}
/** *指定图片的角度* @ param context * @ param bitmap * @ param degree * @ return * /
privatebitmaprotatebitmap (上下文、Bitmap bitmap、float degree ) {
矩阵=新矩阵(;
matrix.reset (;
matrix.setrotate(Degree );
bitmap result=bitmap.create bitmap (bitmap,0,0,bitmap.getWidth )、
bitmap.getHeight (,matrix,true );
返回结果;
}
}
更新不同进程的接口:
这样可以模拟:
定义接口a以更新不同进程的接口b。 将接口a的remoteView传递给接口b,接口b获取对象,调用控件的apply方法更新接口,修改接口a的process属性,并使用不同的进程
接口a :
公共语音发送
remoteviewsremoteviews=newremoteviews (get package name ),r.layout .构件;
remote views.settextviewtext (r.id.msg,'我的信息');
pendingintentpendingintent=pending intent.get activity (this,0,
newintent(this,NotificationActivity.class )、
pending intent.flag _ update _ current;
remote views.setonclickpendingintent (r.id.BTN,pendingintent );
intent intent=new intent (constant.action;
intent.putextra(constant.view,remoteViews );
sendbroadcast(intent );
}
接口B:
privatebroadcastreceivermremotebroadcastreceiver=newbroadcastreceiver (
@Override
公共语音接受(context context,Intent intent ) )。
remoteviewsremoteviews=intent.getparcelableextra (constant.view;
远程视图!=null ) {
view view=remote views.apply (notification activity.this,container );
container.addview(view;
}
}
(;