一、概述
RemoteViews从字面上看是一种远程视图。RemoteViews具有View的结构,既然是远程View,那么它就可以在其他进程中显示。由于它可以跨进程显示,所以为了能够更新他的界面,RemoteViews提供一组基础的操作用于跨进程更新它的UI。
①首先看一下原生的的notification
/** * 显示原生的notification */ protected void showNotification() { // API 16之前的方式好多都过时 Notification.Builder builder = new Notification.Builder(this); builder.setTicker("Hello World!");// 收到通知的时候用于显示于屏幕顶部通知栏的内容 builder.setSmallIcon(R.drawable.ic_launcher);// 设置通知小图标,在下拉之前显示的图标 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));// 落下后显示的图标 builder.setWhen(System.currentTimeMillis()); builder.setContentTitle("This is title"); builder.setContentText("Here is content"); // builder.setSound(uri);//声音提示 // builder.setSound(sound, streamType);//科设置 streamtype // builder.setStyle(style);//style设置 // http://developer.android.com/reference/android/app/Notification.Style.html // builder.setVibrate(long[]);//设置震动 builder.setOngoing(true);// 不能被用户x掉,会一直显示,如音乐播放等 builder.setAutoCancel(true);// 自动取消 builder.setOnlyAlertOnce(true);// 只alert一次 builder.setDefaults(Notification.DEFAULT_ALL); builder.setContentInfo("额外的内容");// 添加到了右下角 Intent intent = new Intent(this, SecondActivity.class); intent.putExtra(SINGLE, NATIVE_NOTIFICATION); intent.setPackage(this.getPackageName()); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); Notification notification = builder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0, notification); }
protected String getCurrentTime() { String time = "unknown"; Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); time = hour + ":" + minute; return time; }
效果展示:
主界面:
点击后的效果
下拉后查看
② 利用RemoteViews自定义通知
protected void showRemoteViewsNotification() { Notification.Builder builder = new Notification.Builder(this); builder.setTicker("Hello RemotesViews!");// 收到通知的时候用于显示于屏幕顶部通知栏的内容 builder.setSmallIcon(R.drawable.ic_launcher);// 设置通知小图标,在下拉之前显示的图标 builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));// 落下后显示的图标 builder.setWhen(System.currentTimeMillis()); builder.setOngoing(true);// 不能被用户x掉,会一直显示,如音乐播放等 builder.setAutoCancel(true);// 自动取消 builder.setOnlyAlertOnce(true);// 只alert一次 builder.setDefaults(Notification.DEFAULT_ALL); mRemoteViews.setImageViewResource(R.id.logo, R.drawable.large_icon); mRemoteViews.setTextViewText(R.id.notify_title, "这是自定义view的title"); mRemoteViews.setTextViewText(R.id.notify_content, "这里是自定义view的内容"); mRemoteViews.setTextViewText(R.id.notify_time, getCurrentTime()); builder.setContent(mRemoteViews); Intent intent = new Intent(this, SecondActivity.class); intent.putExtra(SINGLE, REMOTE_VIEWS_NOTIFICATION); intent.setPackage(this.getPackageName()); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(pendingIntent); Notification notification = builder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0, notification); }