想要看所有设置的请看这一篇 【转】NotificationCopat.Builder所有设置html
经常使用设置:布局
设置属性post |
说明学习 |
setAutoCancel(boolean autocancel)ui |
设置点击信息后自动清除通知this |
setContent(RemoteView view)spa |
设置自定义通知3d |
setContentTitle(String string)code |
设置标题htm |
setContentText(String string) |
设置内容 |
SetContentIntent(PendingIntent intent) |
设置点击信息后的跳转(意图) |
setWhen(long when) |
设置时间 |
setPriority(int pri) |
设置通知的重要程度 |
setStyle(Style style) |
设置样式 |
setVisibility(int visibility) |
设置锁屏显示 |
setDefault(int defaults) |
设置默认 |
setLight(int argb, int onMs, int offMs) |
设置呼吸灯闪烁效果 |
setSound(Uri sound) |
设置通知音效 |
setVibrate(long [] pattern) |
设置震动效果 |
setCategory(String category) |
设置通知类别 |
setColor(int argb) |
设置通知栏颜色 |
setFullScreenIntent(PendingIntent intent,boolean b) |
设置弹窗显示 |
建立一个notification,须要NotificationManager(这是一个系统的服务类,由名字很容易知道是用来管理Notification通知类的)和Notification(通知类)
Notification建立相似Dialog的建立,经过Notification类中提供的各类方法来设置属性,最后build方法生成
NotificationManger的实例能够经过getSystemService这个方法得到
PendingIntent与Intent有关系,这是一个延迟意图,能够经过调用getActivity,getBroadcastReceiver,getService三个方法得到实例
NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULT //通知的重要程度
when: System.currentTimeMillis() //时间
audio stream: STREAM_DEFAULT //音效 系统默认音效
上面的这三个咱们通常状况下能够不用设置,系统会自动帮咱们设置
下面就是一个简单的使用,设置了标题,内容,小图标,点击通知栏的该信息会跳转到main2Activity中,具体能够看注释,以后由这个普通使用的通知做为基础讲解NotificationCopat.Builder中的其余设置
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除,要与setContentIntent连用
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.build();
manger.notify(1,notification);//id为1
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.build();
manger.notify(1,notification);
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.setPriority(NotificationCompat.PRIORITY_MAX) //设置为最高重要程度
.setDefaults(NotificationCompat.DEFAULT_SOUND)//设置音效为默认
.build();
manger.notify(1,notification);
因为只有一条信息,咱们可能看不出最高重要程度的区别,可是,咱们若是再设置音效或者震动,那么这条通知就会弹窗显示
实现弹窗显示的两种方法:一,当重要程度为最高,且设置了音效或者是震动 ;二,调用setFullScreenIntent()
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位图
.setFullScreenIntent(pendingIntent,false)
.build();
manger.notify(1,notification);
图片与以前那张同样,我就不放出来了,值得说的是这个方法的第二个参数,为何是false呢,若是是true的话,当你在玩游戏的时候,屏幕是全屏,而后这条通知就不须要点击,直接就会跳转到某个界面去,除非是来电了才会用true,其余状况如果使用的话,用户估计直接卸载APP
以前也是使用了这个设置与setPriority连用实现了弹窗消息,简单解释,这个能够设置音效,震动和呼吸灯的默认效果,也能够单独设置某个的默认,以前我就是单独设置了音效的默认
有四个参数能够选择,由英文也能够看出是什么意思了吧,这里就很少解释了
这里我就简单地说两种,长文本显示和大图片显示,更多的请看这一篇Android开发——Notification通知的各类Style详解
1、BigTextStyle 长文本显示
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位
.setStyle(new NotificationCompat.BigTextStyle().bigText("长内容长内容长内容长内容长内容长内容长内容长内容长内容长内容长
内容长内容长内容长内容长内容长内容长内容长内容长内容长内容"))
.build();
manger.notify(1,notification);
BigPictureStyle 大图片显示
Intent intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("this is cotenttitle")//设置标题,必要
.setContentText("contentText")//设置内容,必要
.setWhen(System.currentTimeMillis())//设置时间,默认设置,能够忽略
.setSmallIcon(R.mipmap.ic_launcher)//设置通知栏的小图标,必须设置
.setAutoCancel(true)//设置自动删除,点击通知栏信息以后系统会自动将状态栏的通知删除
.setContentIntent(pendingIntent)//设置在通知栏中点击该信息以后的跳转,参数是一个pendingIntent
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//设置大图标,未设置时使用小图标代替,拉下通知栏显示的那个图标
//设置大图片 BitmpFactory.decodeResource(Resource res,int id) 根据给定的资源Id解析成位
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.index)))
.build();
manger.notify(1,notification);
参数是一个RemoteView,由名字大概能够知道是一个View,定义一个布局文件,以后与其绑定,以后,为布局中的按钮设置onClick事件,以后调用setContent,remoteView做为参数传入
RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.notification); remoteView.setOnClickPendingIntent(R.id.last_btn,pendingIntent);
我的以为自定义布局能够作成像音乐播放器的通知栏那样,不过,今天就暂时简单的学习,以后有学到再补充