Notification.builder的简单使用√

第一部分转载自:http://blog.csdn.net/maclay/article/details/8985966

先贴个google的地址 .

此builder为api16后方可使用。

实例的创建与设置

[java]  view plain copy
  1.     //api 16  
  2.         PendingIntent p = PendingIntent.getActivity(this0new Intent(this,MainActivity.class), 0);  
  3.         notification = new Notification.Builder(this)  
  4. //              .addAction(icon, "拉下后的标题", p)//在这条通知下面添加一个icon图标button,点击触发p事件,例如 phone的挂断  
  5.                 .setContentTitle("拉下后的标题")  
  6.                 .setContentText("第一行内容")  
  7. //              .setContent(RemoteViews)//自定义的remoteviews  
  8. //              .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.  
  9. //              .setDeleteIntent(null)// 通知消失时的动作  
  10. //              .setContentIntent(p)//点击的动作 相当于点击button  
  11. //              .setLights(new Color().BLACK, 1000, 1000)//俺的手机不支持..无反应  
  12. //              .setNumber(22)//   
  13. //              .setOngoing(true)//不能被用户x掉,会一直显示,如音乐播放等  
  14. //              .setPriority(2)//优先级  
  15. //              .setProgress(max, progress, indeterminate)//进度条    
  16. //              .setSound(uri)//声音提示  
  17. //              .setSound(sound, streamType)//科设置 streamtype  
  18. //              .setStyle(style)//style设置 http://developer.android.com/reference/android/app/Notification.Style.html  
  19. //              .setVibrate(long[])//设置震动  
  20.                 .setOnlyAlertOnce(true)//一次而已  
  21.                 .setSmallIcon(R.drawable.page)//落下前显示的图标  
  22.                 .setSubText("第二行内容"+"\n"+"!!!!!!!!!!")  
  23.                 .setContentInfo("额外的内容")//添加到了右下角  
  24. //              .setShowWhen(true)//是否允许setwhen  
  25.                 .setWhen(System.currentTimeMillis()+300)//何时通知 这里延迟了300ms,前提设置了setshowwher  
  26.                 .setTicker("直接在标题栏显示的通知"null)  
  27.                 .setLargeIcon(  
  28.                         BitmapFactory.decodeResource(getResources(),  
  29.                                 R.drawable.ic_launcher))//落下后显示的图标  
  30.                 .build();//生成实例 api 16后不再使用getNotification()  

使用 

[java]  view plain copy
  1. mNotificationManager.notify(R.drawable.ic_launcher, notification);  

通用的,摘自 http://www.oschina.net/code/snippet_270292_14489

[java]  view plain copy
  1. // 创建一个NotificationManager的引用     
  2.        NotificationManager notificationManager = (NotificationManager)      
  3.            this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);     
  4.           
  5.        // 定义Notification的各种属性     
  6.        Notification notification =new Notification(R.drawable.icon,     
  7.                "督导系统", System.currentTimeMillis());   
  8.        //FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉  
  9.        //FLAG_NO_CLEAR      该通知不能被状态栏的清除按钮给清除掉  
  10.        //FLAG_ONGOING_EVENT 通知放置在正在运行  
  11.        //FLAG_INSISTENT     是否一直进行,比如音乐一直播放,知道用户响应  
  12.        notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中     
  13.        notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用     
  14.        notification.flags |= Notification.FLAG_SHOW_LIGHTS;     
  15.        //DEFAULT_ALL     使用所有默认值,比如声音,震动,闪屏等等  
  16.        //DEFAULT_LIGHTS  使用默认闪光提示  
  17.        //DEFAULT_SOUNDS  使用默认提示声音  
  18.        //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限  
  19.        notification.defaults = Notification.DEFAULT_LIGHTS;   
  20.        //叠加效果常量  
  21.        //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;  
  22.        notification.ledARGB = Color.BLUE;     
  23.        notification.ledOnMS =5000//闪光时间,毫秒  
  24.           
  25.        // 设置通知的事件消息     
  26.        CharSequence contentTitle ="督导系统标题"// 通知栏标题     
  27.        CharSequence contentText ="督导系统内容"// 通知栏内容     
  28.        Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity     
  29.        PendingIntent contentItent = PendingIntent.getActivity(this0, notificationIntent, 0);     
  30.        notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);     
  31.           
  32.        // 把Notification传递给NotificationManager     
  33.        notificationManager.notify(0, notification);     

第二部分转载自:http://blog.csdn.net/zhumin518/article/details/14000769

notification新旧用法 notification builder

api 11 版本之前:

[java]  view plain copy
  1. //显示通知信息  
  2.     protected void showNotification() {  
  3.         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  4. //              Notification notification = new Notification(this,R.drawable.ic_launcher);  
  5.         Notification notification = new Notification(R.drawable.ic_launcher,"",System.currentTimeMillis() );  
  6.         PendingIntent contentIndent = PendingIntent.getActivity(MainActivity.this0new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);  
  7.         notification.setLatestEventInfo(MainActivity.this"您的BMI值过高""通知监督人", contentIndent);  
  8.         //加i是为了显示多条Notification  
  9.         notificationManager.notify(i,notification);  
  10.         i++;  
  11.     }  


api版本之后:

[java]  view plain copy
  1. protected void showNotification2() {  
  2.         NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  3.         Builder builder = new Notification.Builder(MainActivity.this);  
  4.         PendingIntent contentIndent = PendingIntent.getActivity(MainActivity.this0new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);  
  5.         builder . setContentIntent(contentIndent) .setSmallIcon(R.drawable.ic_launcher)//设置状态栏里面的图标(小图标)                     .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.i5))//下拉下拉列表里面的图标(大图标)        .setTicker("this is bitch!") //设置状态栏的显示的信息  
  6.                .setWhen(System.currentTimeMillis())//设置时间发生时间  
  7.                .setAutoCancel(true)//设置可以清除  
  8.                .setContentTitle("This is ContentTitle")//设置下拉列表里的标题  
  9.                .setContentText("this is ContentText");//设置上下文内容  
  10.         Notification notification = builder.getNotification();  
  11.         //加i是为了显示多条Notification  
  12.         notificationManager.notify(i,notification);  
  13.     }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值