您现在的位置是:首页 > 文章详情

Android收起通知栏--level 16之前的一个坑

日期:2018-02-06点击:550
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~
点击notification时,如果它是发送广播的点击事件,通知栏就不会自动收起。我们需要通过代码手动的让通知栏收起。下面贴一下代码:
[java]  view plain  copy
  1.      /** 
  2.      *  
  3.      * 收起通知栏 
  4.      * @param context 
  5.      */  
  6.     public static void collapseStatusBar(Context context) {  
  7.         try {  
  8.             Object statusBarManager = context.getSystemService("statusbar");  
  9.             Method collapse;    
  10.             if (Build.VERSION.SDK_INT <= 16) {  
  11.                 collapse = statusBarManager.getClass().getMethod("collapse");  
  12.             } else {  
  13.                 collapse = statusBarManager.getClass().getMethod("collapsePanels");  
  14.             }  
  15.             collapse.invoke(statusBarManager);  
  16.         } catch (Exception localException) {  
  17.             localException.printStackTrace();  
  18.         }  
需要加上权限:

[html]  view plain  copy
  1. <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />  

如果点击notification后希望notification消失可以使用以下代码:

[java]  view plain  copy
  1. //清除通知栏中的notification  
  2. NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
  3. notificationManager.cancel(66);//取消通知。66表示想要清除的notification的id。这个是在显示notification时设置的,需要保持一致  

原文链接:https://yq.aliyun.com/articles/465867
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章