一、RemoteViews简介
作用:跨进程更新界面 使用场景:通知栏、桌面小部件
二、在通知栏上的应用
原理:通过RemoteViews加载布局,通过remoteViews的set方法,载入View动态改变布局
(一)、Notifaction的使用:Notification的使用
(二)、利用RemoteView来自定义布局
步骤①、创建layout布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/show_linear" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/show_iv_icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/show_tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World"/> <TextView android:id="@+id/show_tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="I am a super man"/> </LinearLayout> </LinearLayout>
步骤②、利用RemoteViews动态修改布局
Intent intent = new Intent(this,OtherActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); //利用Notification.Builder创建Notification Notification.Builder notification = new Notification.Builder(this); notification.setAutoCancel(true); notification.setSmallIcon(R.mipmap.ic_launcher); notification.setContentTitle("Hello World"); notification.setContentText("I am a ET"); notification.setContentIntent(pendingIntent); /*利用RemoteViews动态修改layout的内容*/ //通过包名,和自定义的layout创建RemoteViews RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification_show); //修改View的内容 remoteViews.setImageViewResource(R.id.show_iv_icon,R.mipmap.ic_launcher); remoteViews.setTextViewText(R.id.show_tv_title,"This is fine day"); remoteViews.setTextViewText(R.id.show_tv_content,"Do you want to go with me"); //对RemoteViews设置跳转 remoteViews.setOnClickPendingIntent(R.id.show_linear,pendingIntent); //将notification的View设置为RemoteViews notification.setContent(remoteViews); notification.setContentIntent(pendingIntent); //配置完成后,将builder转化为Notification Notification notification1 = notification.getNotification(); //放入Notification管理器中 NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0,notification1);
问:为什么更新View需要用到RemoteViews类(稍后讲解)
为什么需要用到PendingIntent。(说明Intent中所包含的意图必须由用户触发)
三、RemoteViews在桌面小部件上的应用(放弃,以后再说)
AppWidgetProvider是Android用于实现桌面小部件的类,本质上是一个广播(BroadcastRecevier)
步骤:①、定义小部件(在res/layout/下新建一个XML文件) 自 定 义
②、定义小部件配置信息(res/xml/下新建一个xml文件)