Notification 用法
版本 Android Studio 1.5.1
API 14 -23
notification 的新旧用法
使用 4.0 的时候要实现方法setLatestEventInfo的时候根本找不到这个方法,在查找官方文档之后再3.0之后系统中的Notification的setLatestEventInfo已经不推荐使用了,所以用Notification.Builder 来代替。
在老的方法中
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"",System.currentTimeMillis() );
PendingIntent contentIndent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(MainActivity.this, "This is content title ", "This is content text ", contentIndent);
notificationManager.notify(1,notification);
在新的方法中
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,0,new Intent(MainActivity.this,MainActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentIntent(contentIntent)
.setContentTitle("下拉后的标题")
.setContentText("第一行的内容")
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setWhen(System.currentTimeMillis())
.setContentInfo("额外的内容")
.setTicker("Ticker")
.build();
manager.notify(1,notification);
以上仅为参考,在不同的版本下Notification使用有一些不同,涉及到改成Builder的使用,总结一下吧:
低于 API Level 11 版本(Android 2.3.3)一下的系统中,setLatestEventInfo()函数是唯一的实现方法。关于其中的属性就不再提了可以查看下方的谷歌官方链接。
高于API Level 11 ,低于 API Level 16(Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使用notification实现。此时前面版本在notification中设置的Flags,icon等属性都已经失效了。要在builder里面设置。
Notification.Builder builder = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setOngoing(true);
notification=builder.getNotification();
高于 API Level 16 的版本,就可以用Builder和build()函数来配套的方便使用notification了。
Notification notification = new Notification.Builder(context)
.setAutoCancel(true)
.setContentTitle("title")
.setContentText("describe")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
这里引用在别处看到的注释
//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()
谷歌官方链接notification.bulider
关注微信公众号,每天都有优质技术文章,搞笑GIF图片推送哦。
2016-7-21
Scott