RemoteViews在Android中的应用场景有两种:通知栏和桌面小部件。
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,名称和内容可以
自定义,看这个小部件要做成什么样子。
<?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,名称随意,添加内容:
<?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: