Notification 介绍
Notification 作为一个事件触发通知型的交互提示接口,让我们可以在获得消息的时候,在状态栏、锁屏界面得到相应的提示信息。从QQ、微信到各种推送通知、短信,这些 Notification 常常出现在状态栏。
Google 在Android 5.0上又进一步改进了通知栏,优化了 Notification。现在,在Android 5.X设备上,一个Notification 界面见下图:
当长按 Notification 的时候会显示该消息来源自哪个应用,见下图:
同时,在 Android 5.X 的设备上,锁屏状态下我们也可以看见 Notification通知,因为使用模拟器的原因,这里就不放图了。
下面我们通过具体代码示例来了解如何在 Android 5.0 下使用 Notification
1. 基本的 Notification
通过 Notification.Builder 创建一个 Notification 的 builder,代码如下:
Notification.Builder builder = new Notification.Builder(this);
它的使用和 AlertDialog 的使用方法非常类似;接下来,给点击的 Notification 后要执行的操作增进一个Intent,由于这个Intent不是马上就执行的,而是由用户触发的,所以Android给这样的Intent 提供了一个 PendingIntent 来帮助我们完成这样的延迟操作。
PendingIntent 的使用非常简单,只需要在普通Intent 的基础上包装一层就可以了,代码如下:
Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"););
//构造 PendingIntent
PendingIntent pendingIntent = new PendingIntent(this,0,intent,0);
这样当点击 Notification 之后,就会触发 PendingIntent事件,打开浏览器开始浏览网页。与使用 AlertDialog 一样,有了 builder 对象,就可以给它增加各种属性。
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentIntent(pendingIntent);//设置点击跳转的Intent
builder.setAutoCancel(true);//设置当用户滑动的时候是否关闭
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标
builder.setContentTitle("我是标题");//设置内容标题
builder.setContentText("我是内容文本");//设置内容文本
builder.setSubText("我是副标题");//设置副标题文本
//builder.setLights();//设置通知LED灯
这些属性相信大家不看注释都能明白是意思意思,最后一步是将 Notification 显示出来,Android 系统通过 NotificationManager 系统服务来帮助我们管理 Notification,并通过 调用 notify 方法来发出 Notification。
/*
显示 Notification
*/
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(1,builder.build());
调用 Notification 的notify方法时,需要传进去一个ID。每个Notification 都会有一个ID,这个ID 就是用来区分不同的APP 的Notification。通过以上很简单的几步,就完成了一个基本的 Notification,最后我们看看效果:
2. 折叠式 Notification
折叠式 Notification 也是一种自定义视图的 Notification,常常用于显示长文本。它拥有两个视图状态,一个是普通状态下的视图状态,另一个是展开状态下的视图状态。在 Notification中,使用 RemoteViews 来帮助我们创建一个自定义的 Notification 视图,代码如下:
//通过RemoteViews来创建自定义的 Notification视图
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.textView,"show me when colapsed");
//通过RemoteViews来创建自定义的 Notification视图(展开后的)
RemoteViews expandedView = new RemoteViews(getPackageName(),R.layout.notification_expanded);
通过如下代码,就可以将一个视图指定为 Notification 正常状态的视图。
notification.contentView = contentView;
将另一个展开的布局通过如下代码指定为展开时的视图。
notification.bigContentView = expandedView;
完整代码如下
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
builder.setContentIntent(pendingIntent); //设置点击跳转的Intent
builder.setAutoCancel(true);//设置当用户滑动的时候是否关闭
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);//通过RemoteViews来创建自定义的 Notification视图
contentView.setTextViewText(R.id.textView,"show me when colapsed");
Notification notification = builder.build();
notification.contentView = contentView;//指定视图
RemoteViews expandedView = new RemoteViews(getPackageName(),R.layout.notification_expanded);//通过RemoteViews来创建自定义的 Notification视图(展开后的)
notification.bigContentView = expandedView;//指定视图
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(2,notification);
折叠状态
展开状态
3. 悬挂式 Notification
悬挂式 Notification 是在 Android 5.0 中新添加的方式,Google 希望通过这种方式来给用户带来更好的体验。这种被称为 Headsup 的 Notification 方式,可以在屏幕上方产生 Notification且不会打断用户操作,能给用户以 Notification 形式的通知。
在Andorid Sample 中,Google 给我们展示了如何完成这样一个效果,代码如下:
Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Headsup Notification")
.setContentText("I am Headsup notification")
.setContentIntent(pendingIntent);
Intent intent = new Intent();
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(this,MainActivity.class);
//如果描述的PendingIntent已经存在,则在产生新的 Intent之前会先取消掉当前的
PendingIntent pendingIntent2 = PendingIntent.getActivity(
this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentText("HeadsUP Notification on Android 5.0");
builder.setFullScreenIntent(pendingIntent2,true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(3,builder.build());
以上代码,通过 setFullScreenIntent ,我们很轻松地将一个 Notification 变成了悬挂式的 Notification ,效果如下:
4. 显示等级的 Notification
Android 5.X 中加入的一种模式 —— Notification 的显示等级。Android 5.X 将 Notification 分成了三个等级。
VISIBILITY_PRIVATE——表明只有当没有锁屏的时候会显示
VISIBILITY_PUBLIC——表明任何情况下都会显示
VISIBILITY_SECRET——表明在 pin、password 等安全锁和没有锁屏的情况下才能够显示
设置 Notification 等级的方式非常简单,同样是借助 builder对象,代码如下:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
我们使用RadioGroup 来演示 VISIBILITY 等级,代码如下:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.visibility_radio_group);
Notification.Builder builder = new Notification.Builder(this)
.setContentTitle("Notification for Visibility test");
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radio_button_public:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText("Public");
builder.setSmallIcon(R.mipmap.ic_public);
break;
case R.id.radio_button_private:
builder.setVisibility(Notification.VISIBILITY_PRIVATE);
builder.setContentText("Private");
builder.setSmallIcon(R.mipmap.ic_private);
break;
case R.id.radio_button_secret:
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setContentText("Secret");
builder.setSmallIcon(R.mipmap.ic_secret);
break;
}
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(4,builder.build());
代码比较简单,通过 builder 对象的 setVisibility 方法,就可以轻松地设置 Notification 的显示等级了。
Notification 在 Android 5.X的改动非常多,比如以下两种:
增加了设置 Notification 背景颜色的接口,代码如下:
builder.setColor(int color);
添加了设置 Notification 的 category 接口,category 用来确定 Notification 显示的位置,参数就是各种 category 的类型,代码如下:
builder.setCategory(Notification.CATEGORY_MESSAGE);