定义:一种可以显示即时信息的控件,该控件显示在标题栏,拉开后会看到通知的完整样式。
Notification和NotificationManager来处理状态栏的推送信息,使用它两发送显示通知分为四个步骤:
①、调用
(NotificationManager) getSystemService(Activity.
NOTIFICATION_SERVICE
)
获取系统的NotificationManager服务
②、创建一个Notification对象,并为其设置属性
③、为Notification对象设置事件信息
④、通过NotificationManager类的notify发送Notification通知
⑤、通过NotificationManager类manager.cancel(NOTIFYID_1);可以清楚通知
获取到Notification对象:
builder
= new Notification.Builder(this):
创建一个创建通知的builder类,在3.0以上的版本可以使用,兼容性低
ncBuilder
= new NotificationCompat.Builder(this):
创建一个创建通知的builder类
,在V4包中,兼容性高
builder
.getNotification()
ncBuilder
.build()
常用方法:
builder
.setSmallIcon(R.drawable.test1):
设置一个状态上的小图标,必须设置
builder
.setTicker("你有一条新消息"):
设置状态栏上的文字
builder
.setContentTitle("来自儿子"):
设置提示标题①
builder
.setContentText("呵呵 我爱你"):
设置提示内容③
builder
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test2)):
设置左边的大图标②
builder
.setWhen(System.currentTimeMillis()):
设置时间⑥
manager
.cancel(10086):
清除对应id通知
manager
.cancelAll()
:
清除所有通知
发送通知:
manager
.notify(10086,
builder
.getNotification())
:
发送通知,第一个是id,
每个通知都有一个唯一的 id
代码示意:
①默认的
builder = new Notification.Builder(this);
// 一定设置小图标 状态上的
builder.setSmallIcon(R.drawable.test1);
// 状态栏上文字
builder.setTicker("你有一条新消息");
// 设置标题图片
builder.setContentTitle("来自信阳儿子");
// 设置提示内容
builder.setContentText("呵呵 我爱你");
// 设置大图标 左边显示
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test2));
// 设置时间
builder.setWhen(System.currentTimeMillis());
// 发送通知
manager.notify(10086, builder.getNotification());
②类似于qq的很多消息的提醒:
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test3).setTicker("你有一条新消息").setContentTitle("来自卡卡西").setContentText("我爱你")
. setLargeIcon ( BitmapFactory . decodeResource ( getResources (), R . drawable . test5 )) . setWhen ( System . currentTimeMillis ());
//
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle(ncBuilder);
for (int i = 0; i < 5; i++) {
style.addLine("haha " + i);
}
//mBuilder.setStyle(inboxStyle);//该行代码存在在与否取决于NotificationCompat.InboxStyle(mBuilder);
manager.notify(10087, ncBuilder.build());
③常驻通知,流氓
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test4).setTicker("你有一条新消息").setContentTitle("来自卡卡西").setContentText("我爱你")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test3))
. setWhen ( System . currentTimeMillis ());
Notification notification = ncBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
manager.notify(10088, notification);
④可以打开应用的通知
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test4).setTicker("你有一条新消息").setContentTitle("来自卡卡西").setContentText("我爱你")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test3))
.setWhen(System.currentTimeMillis());
// hah a
Intent intent = new Intent(this, OpenActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 110, intent, 0);
ncBuilder.setContentIntent(pIntent);
manager.notify(10089, ncBuilder.build());
⑤进度条明显的通知
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test2).setTicker("通知").setContentTitle("数据下载中")
. setLargeIcon ( BitmapFactory . decodeResource ( getResources (), R . drawable . test3 )) . setWhen ( System . currentTimeMillis ());
new Thread() {
public void run() {
for (int i = 0; i <= 100; i++) {
ncBuilder.setProgress(100, i, false).setContentText(i + "%");
// 注意
manager.notify(10090, ncBuilder.build());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
⑥进度条不明显的通知
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test2).setTicker("下载数据").setContentTitle("数据下载中")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.test3))
. setWhen ( System . currentTimeMillis ());ncBuilder.setProgress(100, 0, true).setContentText("数据下载");
manager.notify(10091, ncBuilder.build());
⑦自定义的布局通知
ncBuilder = new NotificationCompat.Builder(this);
ncBuilder.setSmallIcon(R.drawable.test2).setTicker("通知").setContentTitle("数据下载中")
. setLargeIcon ( BitmapFactory . decodeResource ( getResources (), R . drawable . test3 )) . setWhen ( System . currentTimeMillis ());
RemoteViews views = new RemoteViews(getPackageName(), R.layout.open);
ncBuilder.setContent(views);
manager.notify(10092, ncBuilder.build());