Android 获取判断是否有悬浮窗权限的方法
作者:安地Andy 发布时间:2023-11-04 22:48:18
标签:Android,悬浮窗,权限
现在很多应用都会用到悬浮窗,很多国产rom把悬浮窗权限加入控制了,你就需要判断是否有悬浮窗权限,然后做对应操作。
Android 原生有自带权限管理的,只是被隐藏了。看android源码在android.app下就有个AppOpsManager类。
类说明如下:
/**
* API for interacting with "application operation" tracking.
*
* <p>This API is not generally intended for third party application developers; most
* features are only available to system applications. Obtain an instance of it through
* {@link Context#getSystemService(String) Context.getSystemService} with
* {@link Context#APP_OPS_SERVICE Context.APP_OPS_SERVICE}.</p>
*/
上面说明了只对系统应用有用,rom厂商们应该就是利用这个AppOps机制开放一些权限控制。
我们要判断是否有权限该如何做呢?就只能通过反射去判断了。
AppOpsManager的checkOp方法,就是检测是否有某项权限的方法有这些返回值,分别是允许,忽略,错误和默认:
/**
* Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
* allowed to perform the given operation.
*/
public static final int MODE_ALLOWED = 0;
/**
* Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller is
* not allowed to perform the given operation, and this attempt should
* <em>silently fail</em> (it should not cause the app to crash).
*/
public static final int MODE_IGNORED = 1;
/**
* Result from {@link #checkOpNoThrow}, {@link #noteOpNoThrow}, {@link #startOpNoThrow}: the
* given caller is not allowed to perform the given operation, and this attempt should
* cause it to have a fatal error, typically a {@link SecurityException}.
*/
public static final int MODE_ERRORED = 2;
/**
* Result from {@link #checkOp}, {@link #noteOp}, {@link #startOp}: the given caller should
* use its default security check. This mode is not normally used; it should only be used
* with appop permissions, and callers must explicitly check for it and deal with it.
*/
public static final int MODE_DEFAULT = 3;
只有MODE_ALLOWED才是确定有权限的。
类里面checkOp方法如下,三个参数分别是操作id,uid和包名:
/**
* Do a quick check for whether an application might be able to perform an operation.
* This is <em>not</em> a security check; you must use {@link #noteOp(int, int, String)}
* or {@link #startOp(int, int, String)} for your actual security checks, which also
* ensure that the given uid and package name are consistent. This function can just be
* used for a quick check to see if an operation has been disabled for the application,
* as an early reject of some work. This does not modify the time stamp or other data
* about the operation.
* @param op The operation to check. One of the OP_* constants.
* @param uid The user id of the application attempting to perform the operation.
* @param packageName The name of the application attempting to perform the operation.
* @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
* {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
* causing the app to crash).
* @throws SecurityException If the app has been configured to crash on this op.
* @hide
*/
public int checkOp(int op, int uid, String packageName) {
try {
int mode = mService.checkOperation(op, uid, packageName);
if (mode == MODE_ERRORED) {
throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
}
return mode;
} catch (RemoteException e) {
}
return MODE_IGNORED;
}
操作id即op可以在该类中找到静态值定义,android23里面有62种权限,我们需要的是OP_SYSTEM_ALERT_WINDOW=24
知道这些就可以用反射把我们的方法写出了:
/**
* 判断 悬浮窗口权限是否打开
*
* @param context
* @return true 允许 false禁止
*/
public static boolean getAppOps(Context context) {
try {
Object object = context.getSystemService("appops");
if (object == null) {
return false;
}
Class localClass = object.getClass();
Class[] arrayOfClass = new Class[3];
arrayOfClass[0] = Integer.TYPE;
arrayOfClass[1] = Integer.TYPE;
arrayOfClass[2] = String.class;
Method method = localClass.getMethod("checkOp", arrayOfClass);
if (method == null) {
return false;
}
Object[] arrayOfObject1 = new Object[3];
arrayOfObject1[0] = Integer.valueOf(24);
arrayOfObject1[1] = Integer.valueOf(Binder.getCallingUid());
arrayOfObject1[2] = context.getPackageName();
int m = ((Integer) method.invoke(object, arrayOfObject1)).intValue();
return m == AppOpsManager.MODE_ALLOWED;
} catch (Exception ex) {
}
return false;
}
测试在魅族华为小米大部分机型上都是可以的,但这个方法也不能保证正确,一些机型上会返回错误即MODE_ERRORED,就是获取不到权限值,这个方法就返回了false,但实际上悬浮窗是可以使用的。
来源:https://blog.csdn.net/mzm489321926/article/details/50542065


猜你喜欢
- 这将会是一篇比较 * 的文章,当你想在某个人的生活中制造悲剧时你可能会去google搜索它。在Java的世界里,内存溢出仅仅只是你
- 本文实例讲述了Android获取设备CPU核数、时钟频率以及内存大小的方法。分享给大家供大家参考,具体如下:因项目需要,分析了一下 Face
- 关于C# Timer类 在C#里关于定时器类就有3个C# Timer使用的方法1.定义在System.Windows.Forms
- 1、final修饰类被final修饰的类不能被继承,因此final类的成员方法也不能被覆写,被final关键字修饰的类没有子类,因此类的实现
- 本文实例讲述了C#微信公众号与订阅号接口开发示例代码。分享给大家供大家参考,具体如下:using System;using System.W
- 本文实例讲述了Java Swing中JDialog实现用户登陆UI。分享给大家供大家参考,具体如下:JDialog是一种对话框组件,它常常与
- 一、使用@Profile1.1、@Profile修饰类开发环境package com.example.demo.config;import
- FeignClient发送post请求异常这个问题其实很基础。但是却难倒了我。记录一下在发送post请求的时候要指定消息格式正确的写法是这样
- DES一共就有4个参数参与运作:明文、密文、密钥、向量。为了初学者容易理解,可以把4个参数的关系写成:密文=明文+密钥+向量;明文=密文-密
- 实现代码超简单,具体实现方法如下:有时候当我们的游戏人物遇敌时,我们需我怪物随机根据概率选择处理方式,如下:1、50%的机会友好的问候2、2
- SQLite 数据库简介SQLite 是一个轻量级数据库,它是D. Richard Hipp建立的公有领域项目,在2000年发布了第一个版本
- 前言:发送邮件,肯定是每个公司都会有的基本业务。很多公司都会选择把发送邮件作为一个基础服务,对外提供接口。直接调用就可发邮件了。但是我们都知
- openFeign服务间调用保持请求头信息处理1、注意特殊情况,在定时任务或者内部之间调用,没有request的时候,不要处理直接返回。2、
- 概述在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上
- 前言最近做了一个调查问卷导出的功能,需求是将维护的题目,答案,导出成word,参考了几种方案之后,选择功能强大的freemarker+固定格
- maven项目install时忽略执行test在项目所在文件夹根目录使用maven命令打包时<!-- 不执行单元测试,也不编译测试类
- Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, P
- 本文实例讲述了C#简单创建和删除目录的方法。分享给大家供大家参考。具体如下:using System;using System.IO;cla
- 前言作为一个后端程序员,网络连接这块是一个绕不过的砍,当你在做服务器优化的时候,网络优化也是其中一环,那么作为网络连接中最基础的部分-TCP
- Android 检测键盘是否显示及隐藏键盘的方法~~ A