content: /*
   * Copyright  2017 [AllenCoder]
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.allen.supperutils;
  
  import android.annotation.SuppressLint;
  import android.app.AppOpsManager;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.ApplicationInfo;
  import android.os.Build;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Toast;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void onclick(View view) {
          if (!NotificationsUtils.isNotificationEnabled(MainActivity.this)){
              requestPermission(100);
          }
          Toast.makeText(MainActivity.this,"检查权限",Toast.LENGTH_SHORT).show();
      }
      protected void requestPermission(int requestCode) {
          // TODO Auto-generated method stub
          // 6.0以上系统才可以判断权限
  
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) {
              // 进入设置系统应用权限界面
              Intent intent = new Intent(Settings.ACTION_SETTINGS);
              startActivity(intent);
              return;
          } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 运行系统在5.x环境使用
              // 进入设置系统应用权限界面
              Intent intent = new Intent(Settings.ACTION_SETTINGS);
              startActivity(intent);
              return;
          }
          return;
      }
      public static class NotificationsUtils {
          private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
          private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  
          @SuppressLint("NewApi")
          public static boolean isNotificationEnabled(Context context) {
  
              AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
              ApplicationInfo appInfo = context.getApplicationInfo();
              String pkg = context.getApplicationContext().getPackageName();
              int uid = appInfo.uid;
  
              Class appOpsClass = null;
        /* Context.APP_OPS_MANAGER */
              try {
                  appOpsClass = Class.forName(AppOpsManager.class.getName());
                  Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
                          String.class);
                  Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
  
                  int value = (Integer) opPostNotificationValue.get(Integer.class);
                  return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
  
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              } catch (NoSuchMethodException e) {
                  e.printStackTrace();
              } catch (NoSuchFieldException e) {
                  e.printStackTrace();
              } catch (InvocationTargetException e) {
                  e.printStackTrace();
              } catch (IllegalAccessException e) {
                  e.printStackTrace();
              }
              return false;
          }
      }
  }
  