定义:一种可以显示即时信息的控件,该控件显示在标题栏,拉开后会看到通知的完整样式。
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 aIntent 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());
本文详细介绍了Android系统中Notification和NotificationManager的使用方法,包括如何创建、配置、发送及取消通知,提供了从基础到进阶的全面指南。通过示例代码展示了如何实现不同样式的通知,如带有小图标、时间戳、标题、内容、大图标等,以及如何通过Intent触发应用打开。同时,介绍了如何创建常驻通知、带有进度条的通知以及自定义布局的通知,覆盖了通知领域的多种应用场景。
1489

被折叠的 条评论
为什么被折叠?



