先贴个google的地址 .
此builder为api16后方可使用。
实例的创建与设置
//api 16
PendingIntent p = PendingIntent.getActivity(this, 0, new Intent(this,MainActivity.class), 0);
notification = new Notification.Builder(this)
// .addAction(icon, "拉下后的标题", p)//在这条通知下面添加一个icon图标button,点击触发p事件,例如 phone的挂断
.setContentTitle("拉下后的标题")
.setContentText("第一行内容")
// .setContent(RemoteViews)//自定义的remoteviews
// .setFullScreenIntent(p, true)//不会再通知栏直接显示,但拉下后可以显示内容 Only for use with extremely high-priority notifications demanding the user's immediate attention, such as an incoming phone call or alarm clock that the user has explicitly set to a particular time.
// .setDeleteIntent(null)// 通知消失时的动作
// .setContentIntent(p)//点击的动作 相当于点击button
// .setLights(new Color().BLACK, 1000, 1000)//俺的手机不支持..无反应
// .setNumber(22)//
// .setOngoing(true)//不能被用户x掉,会一直显示,如音乐播放等
// .setPriority(2)//优先级
// .setProgress(max, progress, indeterminate)//进度条
// .setSound(uri)//声音提示
// .setSound(sound, streamType)//科设置 streamtype
// .setStyle(style)//style设置 http://developer.android.com/reference/android/app/Notification.Style.html
// .setVibrate(long[])//设置震动
.setOnlyAlertOnce(true)//一次而已
.setSmallIcon(R.drawable.page)//落下前显示的图标
.setSubText("第二行内容"+"\n"+"!!!!!!!!!!")
.setContentInfo("额外的内容")//添加到了右下角
// .setShowWhen(true)//是否允许setwhen
.setWhen(System.currentTimeMillis()+300)//何时通知 这里延迟了300ms,前提设置了setshowwher
.setTicker("直接在标题栏显示的通知", null)
.setLargeIcon(
BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher))//落下后显示的图标
.build();//生成实例 api 16后不再使用getNotification()
使用
mNotificationManager.notify(R.drawable.ic_launcher, notification);
通用的,摘自 http://www.oschina.net/code/snippet_270292_14489
// 创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 定义Notification的各种属性
Notification notification =new Notification(R.drawable.icon,
"督导系统", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
//FLAG_ONGOING_EVENT 通知放置在正在运行
//FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
//DEFAULT_LIGHTS 使用默认闪光提示
//DEFAULT_SOUNDS 使用默认提示声音
//DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限
notification.defaults = Notification.DEFAULT_LIGHTS;
//叠加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //闪光时间,毫秒
// 设置通知的事件消息
CharSequence contentTitle ="督导系统标题"; // 通知栏标题
CharSequence contentText ="督导系统内容"; // 通知栏内容
Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity
PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);
// 把Notification传递给NotificationManager
notificationManager.notify(0, notification);