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 {@Overrideprotected 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});dialog.show();}}

权限管理AppOpsManager相关推荐

  1. Android权限管理原理(含6.0)

    前言 Android系统在MarshMallow之前,权限都是在安装的时候授予的,虽然在4.3时,Google就试图在源码里面引入AppOpsManager来达到动态控制权限的目的,但由于不太成熟,在 ...

  2. Android动态权限管理模型(4.3-6.0)

    Google从4.3开始就试图引入AppOpsManager动态权限管理模型,但是,由于感觉技术不太成熟,在Release版本中,这个功能都是被隐藏掉的,所以官方Rom一直没有动态权限管理机制.直到A ...

  3. 权限管理AppOps

    一 什么叫权限管理? 所谓权限管理,就是能够手动配置某个 App 的权限,进而阻止恶意软件以及防止隐私泄漏.当然,更进一步的权限管理,是能够在 App 动态使用某个权限的时候,弹窗提示用户允许和拒绝. ...

  4. Appops权限管理

    注:本文是根据Android 5.0的代码为基础 1. Appops简介 Appops是Application Operations的简称,是关于应用权限管理的一套方案,但这里的应用指的是系统应用,这 ...

  5. Android原生权限管理:AppOps

    --------------------- 从Android M开始,Google就正式推出了官方的权限管理机制Android Runtime Permission. AppOps终究没有走到台面^^ ...

  6. Android隐藏的权限管理机制:AppOps

    最近整理以前开发中的笔记,发现有点零乱,遂决定将这些笔记整理迁移到 CSDN 上,分享出来与大家一起交流学习.如果有发现不当或有待商榷的地方,欢迎大家拍砖和指正.废话结束,进入本文正题:Android ...

  7. android10管理权限,Android 权限管理

    关于运行时权限 在旧的权限管理系统中,权限仅仅在App安装时询问用户一次,用户同意了这些权限App才能被安装(某些深度定制系统另说),App一旦安装后就可以偷偷的做一些不为人知的事情了. 在Andro ...

  8. android应用权限被默认关闭了,Android 应用权限管理默认开关的修改方法

    修改系统属性:persist.sys.strict_op_enable 开启应用权限管理:true 关闭应用权限管理:false 说明:因为对源码不熟,此部分控制很简单,却走了不少弯路,记之,为大家节 ...

  9. 合肥工业大学—SQL Server数据库实验十:用户及其权限管理

    用户及其权限管理 1. 创建登录名Mylog及密码 2. 创建用户user2关联登录名 3. 创建角色role1 4. 对用户user2及角色role1授权 5. 验证用户授权 6. 收回用户权限 1 ...

最新文章

  1. 中国移动短信网关CMPP3.0 C#源代码:使用示例
  2. objcopy的详细说明
  3. Allegro光绘的导出
  4. SAP CRM BSPWDApplication.do
  5. python地图散点图_在地图上叠加散点图(img)
  6. Java中的正则表达式 - Java Regex示例
  7. java 优秀开源项目
  8. MS AJAX Control Toolkit 学习
  9. gridview 排序
  10. VS2017出现的神奇错误HRSULT:0x80041FE2
  11. 【编译原理】实验二 词法分析程序
  12. linux下phylip软件构建NJ树,SNP数据构建系统进化树
  13. mysql now()函数调用系统时间不对修正方法
  14. 【C语言】输出“*”菱形图案
  15. Ubuntu下载、配置、运行Anaconda
  16. 想拿高工资?Java面试资料集合,附赠课程+题库
  17. selenium被检测
  18. C++ 文件的读写(fin fout)
  19. java-net-php-python-SSM病历管理系统计算机毕业设计程序
  20. js HTML5 网页版植物大战僵尸游戏

热门文章

  1. decimal相关知识
  2. 5月的招聘平淡期,Gtalent如何帮助HR激活人才
  3. 设置Word2007默认打开所有文档的显示比例为100%
  4. 数学知识——余数之和
  5. 摄像头行为分析算法 FPN+PAN
  6. 适当的活动可预防腰间盘突出哦!
  7. 衍生式设计+纤维增强3D打印对汽车零部件进行轻量化设计制造
  8. 全国计算机一级C考试试题,2015全国计算机一级考试试题
  9. 磁卡、条码卡、IC卡、CPU卡、RFID等常识
  10. 2022年“计算机检测维修与数据恢复”国赛模拟赛考试圆满成功