检查android应用权限是否被禁止--悬浮窗权限

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


import android.content.Context;
import android.os.Binder;
import android.os.Build;


public class PermissionCheckUntil {


/**
* 系统权限管理中心服务
*/
    public static final String APP_OPS_SERVICE = "appops";
/**
* 系统悬浮窗权限
*/
public static final int OP_SYSTEM_ALERT_WINDOW = 24;


public static int checkSystemAlterWindow(Context context) {
return checkOp(context, OP_SYSTEM_ALERT_WINDOW);
}

public static int checkNofication(Context context){
return checkOp(context, 25);
}


@SuppressWarnings({ "unchecked", "rawtypes" })
public static int checkOp(Context context, int op) {
final int version = Build.VERSION.SDK_INT;
if (version >= 19) {
Object object = context.getSystemService(APP_OPS_SERVICE);
Class c = object.getClass();
try {
Class[] cArg = new Class[3];
cArg[0] = int.class;
cArg[1] = int.class;
cArg[2] = String.class;
Method lMethod = c.getDeclaredMethod("checkOp", cArg);
return (Integer) lMethod.invoke(object, op,
Binder.getCallingUid(), context.getPackageName());
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return -1;
}
}

你可能感兴趣的:(android应用权限检查)