Notification_安卓

定义:一种可以显示即时信息的控件,该控件显示在标题栏,拉开后会看到通知的完整样式。

Notification和NotificationManager来处理状态栏的推送信息,使用它两发送显示通知分为四个步骤:
    ①、调用 (NotificationManager) getSystemService(Activity. NOTIFICATION_SERVICE ) 获取系统的NotificationManager服务
    ②、创建一个Notification对象,并为其设置属性
    ③、为Notification对象设置事件信息
    ④、通过NotificationManager类的notify发送Notification通知
    ⑤、通过NotificationManager类manager.cancel(NOTIFYID_1);可以清楚通知

一个Notification对象必须包含 setSmallIcon()setContentTitle()setContentText()这三个方法。



获取到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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值