你好,游客 登录 注册 搜索
背景:
阅读新闻

Android NotificationManager与Notification(通知栏)的使用

[日期:2012-11-25] 来源:Linux社区  作者:zyuc_wangxw [字体:大 中 小]

Android NotificationManager与Notification(通知栏)的使用:

有时候我们在后台运行程序,但是需要给用户一个提示,在这个时候就需要使用提示信息了,即在提示栏显示一个图标或者是文字提醒用户。

下面是实现的代码:

  1. protected void showNotification(int id) {         
  2.   CharSequence from = "定位服务";         
  3.   CharSequence message = "正在运行";         
  4.   Intent intent = new Intent();         
  5.   PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);         
  6.   notif.icon = id;         
  7.   notif.setLatestEventInfo(this, from, message, contentIntent);         
  8.   NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);         
  9.   nm.notify(R.string.app_name, notif);     
  10. }

其中ID表示的是图片的ID,文字在这里没有添加,使用的时候按照添加图标的方式可以自己添加!

这样可以更改图片。

下面这个是删除提示的代码:

  1. void delenot() {         
  2.     NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);         
  3.     notificationManager.cancel(R.string.app_name);     
  4.  } 

这样我们添加的提示通知,可以清除掉,如果我们不想让系统清除掉,那么需要设置相关的属性:

notif.flags

这个属性可以设置为不清除或者是加入正在运行的列表。

 

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款