1.什么是RemoteView?

答:其实就是一种特殊的view结构,这种view 能够跨进程传输。并且这种remoteview 还提供了一些方法 可以跨进程更新界面。具体在android里面 一个是通知 一个是桌面小部件。

这2个就是remoteview 最直接的应用了

 

2.RemoteView在通知上的应用?

答:这里给出一个小例子,比较粗糙 仅做演示使用。

 1  //默认样式的notification
 2     private void normalStyleNotification() {
 3         Intent intent = new Intent(MainActivity.this, MainActivity.class);
 4         PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 5         Notification.Builder builder = new Notification.Builder(MainActivity.this);
 6         Notification notification = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").setSmallIcon(R.mipmap.ic_launcher).build();
 7         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 8         manager.notify(1, notification);
 9     }
10 
11     //自定义样式
12     private void customStyleNotification() {
13         Intent intent = new Intent(MainActivity.this, MainActivity.class);
14         PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
15         Notification.Builder builder = new Notification.Builder(MainActivity.this);
16         RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
17         remoteViews.setTextViewText(R.id.tv, "自定义样式的文本");
18         remoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher);
19         Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pendingIntent).setContent(remoteViews).build();
20         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
21         manager.notify(2, notification);
22     }
View Code

相关文章: