zoukankan      html  css  js  c++  java
  • android notification通知学习一二三

    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 使用默认手机震动,需加上<uses-permission android:name="android.permission.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);   

    清除通知:

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

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

    学友资料:http://my.oschina.net/wangjunhe/blog/113855

                              作者:xubuhang                出处:http://www.cnblogs.com/xubuhang/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 

     
查看全文
  • 相关阅读:
    静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?
    编写多线程程序有几种实现方式?
    文件拷贝
    如何实现对象克隆?
    c#如何保存richtextbox的rtf格式
    C#实现文件与二进制互转并存入数据库
    c#中绝对路径和相对路径
    C#实现MySQL数据库中的blob数据存储
    CSS控制文字,超出显示省略号
    ES6 语句判断简写
  • 原文地址:https://www.cnblogs.com/xubuhang/p/4202887.html
  • Copyright © 2011-2022 走看看