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
0
投稿
猜你喜欢
- 本文实例讲述了基于JavaMail API收发邮件的方法。分享给大家供大家参考。具体如下:1.JavaMail API按其功能划分通常可分为
- 简介springmvc对json的前后台传输做了很好封装,避免了重复编码的过程,下面来看看常用的@ResponseBody和@Request
- 在Thread中注入Bean无效在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务。而通过Spring注入
- 我们在java中处理字符串的时候,一般会选择String,在python中同样也是作用于字符串。那么我们今天延伸一下它的用法,只使用Stri
- 本文实例为大家分享了java生成字母验证码的具体代码,供大家参考,具体内容如下import java.awt.BasicStroke;imp
- 简介这篇文章我一直在纠结到底要不要写,不想写一来因为定时器用法比较简单,二来是面试中也不常问。后来还是决定写了主要是想把自己分析问题思路分享
- 一直想在持续集成方向学习并研究一番,近期正准备结合jmeter+ant+jenkins做自动化接口测试,在学习的同时,正好实践一番,毕竟实践
- byte:java中最小的数据类型。1字节/8位。-128(2^7)~127(2^7-1),默认值0。short:短整型,2字节/16位,取
- 一、项目要求实现一个通讯录通讯录可以用来存储100个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址提供方法:添加联系人信息删除指定
- 解决Spring in action @valid验证不生效按照书上的示例代码来实现但是,添加了验证但是没有生效。Spring提供了校验Ap
- 本文实例为大家分享了PropertyDescriptor反射调用set和get方法,供大家参考,具体内容如下第一段:package com.
- @ConditionalOnMissingBean,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的
- 基于SMTP发送一个简单的邮件首先,需要一个认证器:package No001_基于SMTP的文本邮件;import javax.mail.
- 提示:java.util.zipoutputstream
- 一、准备官网下载IntelliJ IDEA 2017 并安装好下载汉化包 (链接: https://pan.baidu.com/s/1JkU
- 1.生成自己的注解(为了确定在哪些位置使用)/** * 关闭patch delete的model处理,否则会报错 */@Target({El
- 1、SDK下载很慢。配置SDK代理,速度像飞一样。建议先把20-24下完,不然后面遇到很多问题。2、support-v7的问题例如res\v
- 前言在mybatis和mybatis plus里,如果你的实体字段是一个枚举类型,而在数据表里是整型,这时在存储时需要进行处理,默认情况下,
- 引言Spring Boot的一个便捷功能是外部化配置,可以轻松访问属性文件中定义的属性。本文将详细介绍@ConfigurationPrope
- 一、SpringBoot是什么Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以