理解RemoteViews
什么是remoteViews?按照字面意思是远程View,但RemoteViews并没有继承View,而是继承Object。它的作用是可以跨进程更新界面,听起来有点神奇。RemoteViews在Android中的使用场景有两种:通知栏和桌面小部件
- RemoteViews的应用
1.在通知栏上的应用
①使用系统默认的样式弹出通知
Notification notification= new Notification();
notification.icon = R.drawable.notify;
notification.when = System.currentTimeMillis();
notification.tickerText = "通知";
notification.flags = Notification.FLAG_AUTO_CANCEL;
//创建延时意图
Intent intent = new Intent(this, OtherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this,"通知", "具体内容", pendingIntent):
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, notification);
②使用RemoteViews可以自定义布局,通过RemoteViews来加载布局文件即可改变通知的样式,具体代码如下
Notification notification= new Notification();
notification.icon = R.drawable.notify;
notification.when = System.currentTimeMillis();
notification.tickerText = "通知";
notification.flags = Notification.FLAG_AUTO_CANCEL;
//创建延时意图
Intent intent = new Intent(this, OtherActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
remoteViews.setTextViewText(R.id.msg, "自定义通知");
remoteViews.setImageViewResource(R.id.icon, R.drawable.icon);
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.btn, pendingIntent2);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(1, notification);
2.在桌面小部件上的应用
AppWidgetProvider是Android中提供的用于实现桌面小部件的类,实际上是一个广播。具体实现步骤如下
①定义小部件界面
自定义xml布局文件,具体布局样式根据实际开发需要,如widget.xml
②定义小部件配置信息
在res/xml下新建appwidget_provider_info.xml,名称任意
③定义小部件的实现类
public class MyAppWidgetProvider extends AppWidgetProvider {
private static final String CLICK_ACTION = "com.hj.action.click";
@Override
public void onReceive(final Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(CLICK_ACTION)) {
new Thread(new Runnable() {
@Override
public void run() {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_launcher_v2);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
for (int i = 0; i < 10; i++) {
float degree = (i * 10) % 360;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setImageViewBitmap(R.id.container, rotateBitmap(context, bitmap, degree));
Intent intent1 = new Intent(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);
remoteViews.setOnClickPendingIntent(R.id.container, pendingIntent);
appWidgetManager.updateAppWidget(new ComponentName(context, MyAppWidgetProvider.class), remoteViews);
SystemClock.sleep(30);
}
}
}).start();
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
int count = appWidgetIds.length; for (int i = 0; i < count; i++) {
int appWidgetId = appWidgetIds[i];
onWidgetUpdate(context, appWidgetManager, appWidgetId);
}
}
private void onWidgetUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent intent = new Intent();
intent.setAction(CLICK_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.container, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
private Bitmap rotateBitmap(Context context, Bitmap bitmap, float degree) {
Matrix matrix = new Matrix();
matrix.reset();
matrix.setRotate(degree);
Bitmap tmpBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return tmpBitmap;
}
}
④在AndroidManifest.xml声明小部件
桌面部件实际上是一个广播组件,必须要注册。APPWIDGET_UPDATE代表该广播是作为小部件的标识存在的
- PendingIntent
PendingIntent表示一种处于pending状态的意图,而pending状态表示的是一种待定、等待、即将发生的意思。PendingIntent支持三种待定意图:启动Activity、启动Service和发送广播。
1.PendingIntent的Flag介绍
FLAG_ONE_SHOT
PendingIntent只能被使用一次,然后会自动cancel;如果后续还有相同的PendingIntent,那么他们的send方法就会调用失败
FLAG_NO_CREATE
PendingIntent不会主动创建,如果当前PendingIntent不存在,那么三种意图方法调用会直接返回null,获取PendingIntent会失败,它无法单独使用
FLAG_CANCEL_CURRENT
PendingIntent如果已经存在,那么它们都会被cancle,然后系统会创建一个新的PendingIntent。对于通知栏而言,那些被cancel的消息单击后无法打开
FLAG_UPDATE_CURRENT
PendingIntent如果已经存在,那么它们都会被更新
通知栏而言,notify(int, notification)方法中,若id值每次都不同的话,需要考虑到flag参数对应消息接收的情况
- RemoteViews内部机制
RemoteViews目前并不能支持所有的View的类型,不能支持自定义View、EditText等,同时没有提供findViewById方法,无法直接访问View元素。RemoteViews会通过Binder传递到SystemServer进程,系统会通过RemoteViews中包名等信息去得到该应用的资源并加载布局文件,当需要更新RemoteViews时,我们需要通过一系列set方法并通过NotificationManager和AppWidgetManager来提交更新任务,具体的更新操作也是在SystemServer进程中完成的。RemoteViews提供一个Action的概念,Action代表一个View操作,系统首先将View操作封装到Action对象并跨进程传输到远程进程,接着在远程进程中执行Action对象中的具体操作