android开发之Notification结合PendingIntent使用小结

一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。发现这个功能特别好用。

使用小结:

1。获得系统服务NotificationMangaer:
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


2。创建Notification对象  【三个参数的意义分别为,通知的图片,状态栏中通知的滚动信息,什么时候展示该通知。】
    Notification notification = new Notification(R.drawable.ic_launcher, "通知栏标题", System.currentTimeMillis());


3。设置flags参数:
    notification.flags = Notification.FLAG_AUTO_CANCEL;     // Notification.FLAG_NO_CLEAR【表明在点击了通知栏中的"清除通知"后,此通知不清除】
   通知时发出声音
    notification.defaults = Notification.DEFAULT_SOUND;


4。创建intent对象:
    Intent intent = new Intent(MainActivity.this, Notifications.class);


    如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
    
   |————>Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例。
    |
    |
    |————>Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的 |     Task,并在该新建的Task上创建Activity。


5。 通过context对象和intent对象创建PendingIntent:
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0, intent, 0);


6.设置下拉框信息:【参数依次为:Context,标题,内容,PendingIntent。】

notification.setLatestEventInfo(MainActivity.this,"下拉框通知的标题","下拉框通知的内容", pendingIntent);

7.启动Notification:


    manager.notify(0, notification);
          
参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个    Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值