美文网首页
[Android]展开/收起通知栏

[Android]展开/收起通知栏

作者: 黑森林中的小木屋 | 来源:发表于2018-08-16 11:03 被阅读0次

需要权限:
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

收起通知栏(经测能用)

public  void collapseStatusBar() {
        Object service =getSystemService("statusbar");
        if (null == service)
            return;
        try {
            Class<?> clazz = Class.forName("android.app.StatusBarManager");
            int sdkVersion = android.os.Build.VERSION.SDK_INT;
            Method collapse = null;
            if (sdkVersion <= 16) {
                collapse = clazz.getMethod("collapse");
            } else {
                collapse = clazz.getMethod("collapsePanels");
            }
            collapse.setAccessible(true);
            collapse.invoke(service);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

展开通知栏(未测试)

/**
    * 展开通知栏
    * @param context
     */
   public static void expandNotification(Context context) {
      Object service = context.getSystemService("statusbar");
      if (null == service)
         return;
      try {
         Class<?> clazz = Class.forName("android.app.StatusBarManager");
         int sdkVersion = android.os.Build.VERSION.SDK_INT;
         Method expand = null;
         if (sdkVersion <= 16) {
            expand = clazz.getDeclaredMethod("expand");
         } else {
          /*
           * Android SDK 16之后的版本展开通知栏有两个接口可以处理
           * expandNotificationsPanel()
           * expandSettingsPanel()
           */
            //expand =clazz.getMethod("expandNotificationsPanel");
            expand = clazz.getDeclaredMethod("expandSettingsPanel");
         }
         expand.setAccessible(true);
         expand.invoke(service);
      } catch (Exception e) {
//       //e.printStackTrace();
      }
   }

转自文章:折叠与展开通知栏方法

相关文章

网友评论

      本文标题:[Android]展开/收起通知栏

      本文链接:https://www.haomeiwen.com/subject/ulunbftx.html