现在很多应用都会用到悬浮窗,很多国产rom把悬浮窗权限加入控制了,你就需要判断是否有悬浮窗权限,然后做对应操作。

Android 原生有自带权限管理的,只是被隐藏了。看android源码在android.app下就有个AppOpsManager类。

类说明如下:

/**

* API for interacting with "application operation" tracking.

*

*

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}.

*/

上面说明了只对系统应用有用,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

* silently fail (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 not 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,但实际上悬浮窗是可以使用的。

以上这篇Android 获取判断是否有悬浮窗权限的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

android悬浮动态权限,Android 获取判断是否有悬浮窗权限的方法相关推荐

  1. Android检测是否有悬浮窗,Android 获取判断是否有悬浮窗权限的方法

    现在很多应用都会用到悬浮窗,很多国产rom把悬浮窗权限加入控制了,你就需要判断是否有悬浮窗权限,然后做对应操作. Android 原生有自带权限管理的,只是被隐藏了.看android源码在androi ...

  2. Android实现动态贴纸,Android开发之仿微博贴纸效果实现——进阶篇

    上个月写了一篇<Android开发之仿微博贴纸效果实现--基础篇>,文章中提到还有一篇进阶篇要写,很早就想动笔了,因中途去维护了开源库<高仿微信图片选择器2.0版本>,导致耽搁 ...

  3. android的动态注册,Android应用开发之BroadcastReceiver(广播)的静态注册和动态注册 --Android开发...

    本文将带你了解Android应用开发之BroadcastReceiver(广播)的静态注册和动态注册 --Android开发,希望本文对大家学Android有所帮助 BroadcastReceiver ...

  4. android fragment动态添加,Android动态添加Fragment

    效果图如下: 项目结构图如下: Fragment1: package com.demo.dongtaifragment; import android.app.Fragment; import and ...

  5. android 设置动态头像,Android实现动态圆环的图片头像控件

    先看效果图: 现在大部分的app上难免会使用到圆形头像,所以今天我给大家分享一个单独使用的,并且周围带有圆环动画的花哨圆形头像控件,本控件是在圆形头像控件基础上实现的,只是在其周围再画一些不同大小的圆 ...

  6. android status_bar_height动态调整,Android沉浸状态栏(StatusBar)兼容方案

    所谓"沉浸状态栏"的实现需要两点: 设置状态栏为透明或者半透明状态; 整体布局可以置于状态栏下方. 1. 状态栏的配置 对于状态栏的配置有两种方式: 在manifest中配置Act ...

  7. android popupmenu 动态添加,Android简单实现列表菜单--PopupMenu的简单使用。

    最近发现PopupMenu用来做菜单非常的方便,而且使用也非常的简单,下面我们就来看下如何使用的吧. 首先在布局文件中创建两个Button. xmlns:android="http://sc ...

  8. android textview动态设置,android – 如何动态设置文本到TextView?

    我想在活动开始时显示连接的ssid和ip地址.它包含一个图像视图(用于标题,因为我没有使用操作栏)和4个文本视图(ssid标签,ssid值,ipaddr标签和ipaddr值) 我已经测试了获取ssid ...

  9. android 时钟动态图标,Android 8.1 Launcher3实现动态指针时钟功能

    本文主要实现功能,可能有不合理的地方 首先创建一个实现功能的工具里,直接上代码: import android.content.Context; import android.graphics.Bit ...

最新文章

  1. 机器学习算法学习---模型融合和提升的算法(五)
  2. 超级数学计算机,超级计算器+
  3. awesome docsify learning notes
  4. 北京内推 | 腾讯云小微自然语言技术中心招聘NLP研究型实习生
  5. github里的默认域_恕我直言!你对Python里的import一无所知
  6. Python 爬虫面试题 170 道:2019 版
  7. 代码编辑器VS Code的“Chromium”版来啦:安全、开源、保护你的隐私
  8. 瑞典皇家理工学院工程类表
  9. 选择排序详解(Java实现)
  10. 关于java调用Dll文件的异常 Native library (win32-x86-64/CtrlNPCDLL.dll) not found in resource pat...
  11. 小程序和Android开发,微信小程序和Android开发的对比
  12. 服务器虚拟机系统镜像安装win7系统,在虚拟机中怎么安装Win7旗舰版系统
  13. 【HTML基础】acronym和abbr的区别
  14. cesium 设置飞机的heading pitch roll(航向等)
  15. 程序猿的情怀 语录(三)
  16. 利用pytorch实现平均绝对值误差(MAE)
  17. 查看依赖关系 dependency walker(depends)
  18. 呀,葵花宝典![IT项目经理成长晋升记2]
  19. hihocoder 1041
  20. lua根据权重随机test

热门文章

  1. linux 日记函数,每日记一些php函数,jQuery函数和linux命令(一)
  2. 测试boot库下I/O模型类型
  3. 4 error C2220: 警告被视为错误 - 没有生成“object”文件 (..\..\src\caffe\util\math_functions.cpp)
  4. python 根据名称获取pid_【Python学习笔记】76、常用第三方模块psutil
  5. sensei鼠标测试软件,'黑科技'传感器打造出的FPS利器 - 赛睿Sensei 310 鼠标
  6. docker公共存储库_查找并修复docker镜像安全漏洞
  7. PHP制作订货,PHP生成订单号的两种方法
  8. mysql info commit_mysql show processlist 发现大量的commit
  9. java修改原有txt文件_(转)Java创建txt文件并进行读、写、修改操作
  10. Win11桌面没有图标怎么解决 Win11桌面没有图标解决教程