博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android notification通知学习一二三
阅读量:7033 次
发布时间:2019-06-28

本文共 2988 字,大约阅读时间需要 9 分钟。

Notification

可以用来在状态栏上显示图标和信息、使LED灯闪烁、震动手机、铃声或者音乐提醒、显示额外的信息、使用可交互的控制手段。

一个notification对象必须包含如下内容:

小图标(setSmallIcon()获取)

标题(setContentTitle()获取)

详情文字(setContentText()获取)

 除此之外,其余内容都是可选的,要了解这些可选的内容参考NotificationCompat.Builder的文档。

Notification Manager是用来管理Notification,包括触发一个新的Notification、修改存在的、取消Notifications

NotificationManager常用方法介绍:
显示方法
public  
void  
notify(
int 
id, Notification notification) 将通知加入状态栏,标记为id
public 
void   
notify(String tag ,
int
id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
清除方法
public  
void  
cancel(
int 
id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
public  
void   
cancelAll() 移除所有通知(只是针对当前Context下的Notification)

核心代码:

// 创建一个NotificationManager的引用           NotificationManager notificationManager = (NotificationManager)                this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);                     // 定义Notification的各种属性           Notification notification =new Notification(R.drawable.icon_luncher,                   "通知", System.currentTimeMillis());         //FLAG_AUTO_CANCEL   该通知能被状态栏的清除按钮给清除掉        //FLAG_NO_CLEAR      该通知不能被状态栏的清除按钮给清除掉        //FLAG_ONGOING_EVENT 通知放置在正在运行        //FLAG_INSISTENT     是否一直进行,比如音乐一直播放,知道用户响应        notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中           notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用           notification.flags |= Notification.FLAG_SHOW_LIGHTS;           //DEFAULT_ALL     使用所有默认值,比如声音,震动,闪屏等等        //DEFAULT_LIGHTS  使用默认闪光提示        //DEFAULT_SOUNDS  使用默认提示声音

            //添加声音notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

            //notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

            //如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"

  //如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
     //DEFAULT_VIBRATE 使用默认手机震动,需加上
权限 notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND; notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //闪光时间,毫秒 // 设置通知的事件消息 CharSequence contentTitle ="我是标题"; // 通知栏标题 CharSequence contentText ="我是内容"; // 通知栏内容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager notificationManager.notify(0, notification);

清除通知:

// 清除NotificationNotificationManager    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);nm.cancel(0);//数字0是清除的标识符与显示的标识符相同

注:清除动作一般在进入指定页面前清除掉,可以在onResume()或者onStart()里面执行。

学友资料:

转载于:https://www.cnblogs.com/xubuhang/p/4202887.html

你可能感兴趣的文章
Masonry整理
查看>>
世界之大,无不分层
查看>>
linux redhat5+11g
查看>>
centOS7 安装 JAVA环境
查看>>
测试博文
查看>>
Miller-Rabin随机性素数测试算法(Miller_Rabin模板)
查看>>
转eclipse failed to create the java virtual machine
查看>>
研究float的一些好文章
查看>>
我的友情链接
查看>>
TCP/IP(二) —— TCP 概述
查看>>
ROS-Indigo版在Ubuntu上的安装
查看>>
Spark for Spatial,相关资源
查看>>
oracle数据导入导出
查看>>
Flask-RESTful构建小型REST服务
查看>>
LB集群--LVS部署
查看>>
AIX磁带备份
查看>>
ELK 6.4 实时日志分析系统
查看>>
Zend Studio使用教程之在Linux上进行安装
查看>>
linux下上传本地文件至github
查看>>
Android VelocityTracker
查看>>