zoukankan      html  css  js  c++  java
  • Android Notification通知

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


      //删除通知    
        private void clearNotification(){
            // 启动后删除之前我们定义的通知  
            NotificationManager notificationManager = (NotificationManager) this
                    .getSystemService(NOTIFICATION_SERVICE);   
            notificationManager.cancel(0);  
     
        }
    NotificationManager常用方法介绍:
    public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)
    public  void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)
    public  void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id
    public  void notify(int id, Notification notification) 将通知加入状态栏,标记为id
         
  • 相关阅读:
    DECLARE_MESSAGE_MAP用法
    BEGIN_MESSAGE_MAP
    2019-2020-1 20175314 《信息安全系统设计基础》第2周学习总结
    2019-2020-1 20175314 《信息安全系统设计基础》第1周学习总结
    20175314 实验五 Java网络编程
    20175314 实验四 Android开发基础
    20175314 《Java程序设计》第十一周学习总结
    20175314薛勐 数据结构-排序(选做)
    20175314薛勐 数据结构-单链表(选做)
    20175314 《Java程序设计》第十周学习总结
  • 原文地址:https://www.cnblogs.com/java-g/p/4126576.html
Copyright © 2011-2022 走看看