权限管理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();
} }
权限管理AppOpsManager的更多相关文章
- [Android Pro] android 4.4 Android原生权限管理:AppOps
reference : http://m.blog.csdn.net/blog/langzxz/45308199 reference : http://blog.csdn.net/hyhyl1990/ ...
- Android权限管理知识学习记录
一.Android权限背景知识 在Android 6.0之前,所申请的权限只需要在AndroidManifest.xml列举就可以了,从而容易导致一些安全隐患,因此,在Android 6.0时,Goo ...
- Android 开发者必知必会的权限管理知识
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/OQRHEufCUXBA3d3DMZXMKQ 导语 本 ...
- Android权限管理之RxPermission解决Android 6.0 适配问题
前言: 上篇重点学习了Android 6.0的运行时权限,今天还是围绕着Android 6.0权限适配来总结学习,这里主要介绍一下我们公司解决Android 6.0权限适配的方案:RxJava+RxP ...
- Android权限管理之Android 6.0运行时权限及解决办法
前言: 今天还是围绕着最近面试的一个热门话题Android 6.0权限适配来总结学习,其实Android 6.0权限适配我们公司是在今年5月份才开始做,算是比较晚的吧,不过现在Android 6.0以 ...
- Android权限管理之Permission权限机制及使用
前言: 最近突然喜欢上一句诗:"宠辱不惊,看庭前花开花落:去留无意,望天空云卷云舒." 哈哈~,这个和今天的主题无关,最近只要不学习总觉得生活中少了点什么,所以想着围绕着最近面试过 ...
- SpringMVC+Shiro权限管理【转】
1.权限的简单描述 2.实例表结构及内容及POJO 3.Shiro-pom.xml 4.Shiro-web.xml 5.Shiro-MyShiro-权限认证,登录认证层 6.Shiro-applica ...
- Android6.0运行时权限管理
自从Android6.0发布以来,在权限上做出了很大的变动,不再是之前的只要在manifest设置就可以任意获取权限,而是更加的注重用户的隐私和体验,不会再强迫用户因拒绝不该拥有的权限而导致的无法安装 ...
- Oracle 表空间和用户权限管理
一. 表空间 Oracle数据库包含逻辑结构和物理结构. 数据库的物理结构指的是构成数据库的一组操作系统文件. 数据库的逻辑结构是指描述数据组织方式的一组逻辑概念以及它们之间的关系. 表空间是数据库逻 ...
随机推荐
- 21. javacript高级程序设计-Ajax与Comet
1. Ajax与Comet 1.1 XMLHttpRequest对象 IE5是第一款引入XHR对象的浏览器,IE5中是通过MSXML库中的一个ActiveX对象实现的.因此在IE中可能存在MSXML2 ...
- Java for LeetCode 218 The Skyline Problem【HARD】
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- [转]Git远程操作详解
原文:http://www.ruanyifeng.com/blog/2014/06/git_remote.html Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多 ...
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
- Qt 获取Mac地址
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces(); foreach(QNetworkInterface ...
- iOS中UITableView的一些设置
不可滑动: ? 1 tableView.userInteractionEnabled = NO; 也可以在storyboard中的userInteractionEnable属性设置 显示导向箭头: ? ...
- iOS开发人员不容错过的10大工具
内容简介 1.iOS简介 2.iOS开发十大实用工具之开发环境 3.iOS开发十大实用工具之图标设计 4.iOS开发十大实用工具之原型设计 5.iOS开发十大实用工具之演示工具 6.iOS开发十大实用 ...
- Vi 的基本使用
一.Vi入门 Unix 提供了全屏幕的Vi编辑器,这使我们的工作轻松不少.不少DOS用户抱怨Vi编辑器不象DOS下的编辑器如edit那么好用,这 是因为Vi考虑到各种用户的需要,没有使用某些通用的编 ...
- Delphi面向对象的可见性表示符
Delphi能通过在声明域和方法的时候用protected.private.public.published和automated指示符来对对象提供进一步的控制.使用这些关键字的语法如下 TSomeOb ...
- 【Java EE 学习 21 下】【使用java实现邮件发送、邮件验证】
一.邮件发送 1.邮件发送使用SMTP协议或者IMAP协议,这里使用SMTP协议演示. SMTP协议使用的端口号:25 rfc821详细记载了该协议的相关信息 (1)使用telnet发送邮件(使用12 ...