Notification_安卓
来源:互联网 发布:淘宝购买叶罗丽娃娃 编辑:程序博客网 时间:2024/05/01 12:14
定义:一种可以显示即时信息的控件,该控件显示在标题栏,拉开后会看到通知的完整样式。
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());
0 0
- Notification_安卓
- android开发之Notification_通知栏消息
- android开发之Notification_通知栏消息
- android开发之Notification_通知栏消息
- android开发之Notification_通知栏消息
- 安卓
- 安卓
- 安卓
- 安卓
- 【安卓】
- 安卓
- 安卓
- 安卓
- 安卓
- 安卓
- 安卓
- 安卓
- 安卓
- TCP之深入浅出send和recv
- PAT 1019. General Palindromic Number (20)
- python正则表达式
- python 随机产生多维高斯分布点
- hdu3646 Fate Stay Night(dp,读题障碍)
- Notification_安卓
- HDU 5533 Dancing Stars on Me(判断n个整数点是否能组成正n边形)——2015ACM/ICPC亚洲区长春站
- Longest Common Prefix
- HDOJ 1995 汉诺塔V
- 粒子群算法的matlab实现
- 【程序44】 题目:一个偶数总能表示为两个素数之和。
- 拼图小游戏“ST--拼图”开发篇之主要功能的实现(三)
- AlertDialog的初步了解
- 新手必看之UILabel