在早些的版本中,我们通常会new一个Notification,然后设置它的icon、when、flags等属性,最后用NotificationManager将其显示出来。现在对Notification直接设置属性的API已被Google标识为过时,更推荐使用NotificationCompact.Builder来构建一个通知。下边的代码是一个简单的通知示例:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, MainActivity.class);//点击之后进入MainActivity
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
.setTicker("hello world")//设置文字
.setWhen(System.currentTimeMillis())//通知的时间
.setAutoCancel(true)//点击后消失
.setContentIntent(pendingIntent)//设置意图
.build();//创建通知对象完成
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);//显示通知
上述代码的实现是使用默认的通知样式,如果我们想自定义通知的布局,需要用到RemoteViews。我们先写好自定义通知的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="vertical"
android:paddingEnd="48dp"
android:paddingStart="48dp">
<ImageView
android:id="@+id/icon_5"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/tv_5_s"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:gravity="center_vertical"
android:text="Left" />
<TextView
android:id="@+id/tv_5_e"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:text="Right" />
</RelativeLayout>
之后我们来看看怎么将自定义的布局运用在通知中:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notificaiton);
remoteViews.setTextViewText(R.id.tv_5_s, "Start");//通过id-内容的方式设置remoteview中控件的内容,底层实现是通过Binder跨进程通信
remoteViews.setTextViewText(R.id.tv_5_e, "End");
remoteViews.setImageViewResource(R.id.icon_5, R.mipmap.ic_launcher);
remoteViews.setOnClickPendingIntent(R.id.tv_5_e, pendingIntent);
Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher)//通知的构建过程基本与默认相同
.setTicker("hello world")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContent(remoteViews)//在这里设置自定义通知的内容
.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, notification);
可以看到Remoteview更新内容的方式比较特别,因为不能通过findViewById的方式获取控件(跨进程了获取不到),所以更新实际上是通过Binder发送更新信息到remoteview来更新UI的。
除了用来自定义通知栏内容,Remoteview还可以用来实现桌面小插件,相关内容将在下一篇进行讨论。