RemoteViews提供了一组操作用于跨进程更新界面,在Android中主要应用在通知栏和桌面小部件。
使用系统默认样式弹出通知栏
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("通知,通知");
builder.setContentInfo("这是一个通知,点击可以跳转到Main2Activity");
builder.setWhen(System.currentTimeMillis());
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.getNotification());
使用RemoteViews弹出自定义通知栏,这里必须要setIcon,否则通知不显示
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setTextViewText(R.id.textView, "老板黄鹤带着他的小姨子跑路了,黄鹤你他妈的不是人");
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);