权限管理AppOpsManager

AppOps工具类

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.os.Binder;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

/**
 * 应用程序被禁用项判断,如:是否禁止在通知栏显示通知、是否禁用悬浮窗
 * DateTime:2016/6/15 23:17
 * Builder:Android Studio
 *
 * @see android.app.AppOpsManager
 */
public class AppOpsUtils {
    public static final int OP_NONE = -1;
    public static final int OP_COARSE_LOCATION = 0;
    public static final int OP_FINE_LOCATION = 1;
    public static final int OP_GPS = 2;
    public static final int OP_VIBRATE = 3;
    public static final int OP_READ_CONTACTS = 4;
    public static final int OP_WRITE_CONTACTS = 5;
    public static final int OP_READ_CALL_LOG = 6;
    public static final int OP_WRITE_CALL_LOG = 7;
    public static final int OP_READ_CALENDAR = 8;
    public static final int OP_WRITE_CALENDAR = 9;
    public static final int OP_WIFI_SCAN = 10;
    public static final int OP_POST_NOTIFICATION = 11;
    public static final int OP_NEIGHBORING_CELLS = 12;
    public static final int OP_CALL_PHONE = 13;
    public static final int OP_READ_SMS = 14;
    public static final int OP_WRITE_SMS = 15;
    public static final int OP_RECEIVE_SMS = 16;
    public static final int OP_RECEIVE_EMERGECY_SMS = 17;
    public static final int OP_RECEIVE_MMS = 18;
    public static final int OP_RECEIVE_WAP_PUSH = 19;
    public static final int OP_SEND_SMS = 20;
    public static final int OP_READ_ICC_SMS = 21;
    public static final int OP_WRITE_ICC_SMS = 22;
    public static final int OP_WRITE_SETTINGS = 23;
    public static final int OP_SYSTEM_ALERT_WINDOW = 24;
    public static final int OP_ACCESS_NOTIFICATIONS = 25;
    public static final int OP_CAMERA = 26;
    public static final int OP_RECORD_AUDIO = 27;
    public static final int OP_PLAY_AUDIO = 28;
    public static final int OP_READ_CLIPBOARD = 29;
    public static final int OP_WRITE_CLIPBOARD = 30;
    public static final int OP_TAKE_MEDIA_BUTTONS = 31;
    public static final int OP_TAKE_AUDIO_FOCUS = 32;
    public static final int OP_AUDIO_MASTER_VOLUME = 33;
    public static final int OP_AUDIO_VOICE_VOLUME = 34;
    public static final int OP_AUDIO_RING_VOLUME = 35;
    public static final int OP_AUDIO_MEDIA_VOLUME = 36;
    public static final int OP_AUDIO_ALARM_VOLUME = 37;
    public static final int OP_AUDIO_NOTIFICATION_VOLUME = 38;
    public static final int OP_AUDIO_BLUETOOTH_VOLUME = 39;
    public static final int OP_WAKE_LOCK = 40;
    public static final int OP_MONITOR_LOCATION = 41;
    public static final int OP_MONITOR_HIGH_POWER_LOCATION = 42;
    public static final int OP_GET_USAGE_STATS = 43;
    public static final int OP_MUTE_MICROPHONE = 44;
    public static final int OP_TOAST_WINDOW = 45;
    public static final int OP_PROJECT_MEDIA = 46;
    public static final int OP_ACTIVATE_VPN = 47;
    public static final int OP_WRITE_WALLPAPER = 48;
    public static final int OP_ASSIST_STRUCTURE = 49;
    public static final int OP_ASSIST_SCREENSHOT = 50;
    public static final int OP_READ_PHONE_STATE = 51;
    public static final int OP_ADD_VOICEMAIL = 52;
    public static final int OP_USE_SIP = 53;
    public static final int OP_PROCESS_OUTGOING_CALLS = 54;
    public static final int OP_USE_FINGERPRINT = 55;
    public static final int OP_BODY_SENSORS = 56;
    public static final int OP_READ_CELL_BROADCASTS = 57;
    public static final int OP_MOCK_LOCATION = 58;
    public static final int OP_READ_EXTERNAL_STORAGE = 59;
    public static final int OP_WRITE_EXTERNAL_STORAGE = 60;
    public static final int OP_TURN_SCREEN_ON = 61;
    private static final String TAG = "liyujiang";

    /**
    * 是否禁用通知
    */
    public static boolean allowNotification(Context context) {
        return isAllowed(context, OP_POST_NOTIFICATION);
    }

    /**
    * 是否禁用悬浮窗
    */
    public static boolean allowFloatWindow(Context context) {
        return isAllowed(context, OP_SYSTEM_ALERT_WINDOW);
    }

    /**
    * 是否禁用某项操作
    */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean isAllowed(Context context, int op) {
        Log.d(TAG, "api level: " + Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT < 19) {
            return true;
        }
        Log.d(TAG, "op is " + op);
        String packageName = context.getApplicationContext().getPackageName();
        AppOpsManager aom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Class<?>[] types = new Class[]{int.class, int.class, String.class};
        Object[] args = new Object[]{op, Binder.getCallingUid(), packageName};
        try {
            Method method = aom.getClass().getDeclaredMethod("checkOpNoThrow", types);
            Object mode = method.invoke(aom, args);
            Log.d(TAG, "invoke checkOpNoThrow: " + mode);
            if ((mode instanceof Integer) && ((Integer) mode == AppOpsManager.MODE_ALLOWED)) {
                Log.d(TAG, "allowed");
                return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "invoke error: " + e);
            e.printStackTrace();
        }
        return false;
    }

}

Activity判断

/**
* 经测试,部分手机(如华为mate7、华为x1)无法识别出定位是否被禁用,禁用后总是识别为已允许,暂无解决方案。
*/
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onOpCheck(View view) {
        String msg = "";
        if (AppOpsUtils.allowNotification(this)) {
            msg += "允许:显示通知到通知栏\n";
        } else {
            msg += "禁止:显示通知到通知栏\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_FINE_LOCATION)) {
            msg += "允许:OP_FINE_LOCATION\n";
        } else {
            msg += "禁止:OP_FINE_LOCATION\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_COARSE_LOCATION)) {
            msg += "允许:OP_COARSE_LOCATION\n";
        } else {
            msg += "禁止:OP_COARSE_LOCATION\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_GPS)) {
            msg += "允许:OP_GPS\n";
        } else {
            msg += "禁止:OP_GPS\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_MONITOR_LOCATION)) {
            msg += "允许:OP_MONITOR_LOCATION\n";
        } else {
            msg += "禁止:OP_MONITOR_LOCATION\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_MONITOR_HIGH_POWER_LOCATION)) {
            msg += "允许:OP_MONITOR_HIGH_POWER_LOCATION\n";
        } else {
            msg += "禁止:OP_MONITOR_HIGH_POWER_LOCATION\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_READ_PHONE_STATE)) {
            msg += "允许:OP_READ_PHONE_STATE\n";
        } else {
            msg += "禁止:OP_READ_PHONE_STATE\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_READ_EXTERNAL_STORAGE)) {
            msg += "允许:OP_READ_EXTERNAL_STORAGE\n";
        } else {
            msg += "禁止:OP_READ_EXTERNAL_STORAGE\n";
        }
        if (AppOpsUtils.isAllowed(this, AppOpsUtils.OP_WRITE_EXTERNAL_STORAGE)) {
            msg += "允许:OP_WRITE_EXTERNAL_STORAGE\n";
        } else {
            msg += "禁止:OP_WRITE_EXTERNAL_STORAGE\n";
        }
        showAlert(msg);
    }

    private void showAlert(String msg) {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage(msg);
        dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

}

 

转载于:https://www.cnblogs.com/anni-qianqian/p/5775394.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值