Android 获取判断是否有悬浮窗权限的方法

 更新时间:2018年07月31日 11:21:13   作者:安地Andy  
今天小编就为大家分享一篇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,但实际上悬浮窗是可以使用的。

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

相关文章

  • 详解Android 7.0 Settings 加载选项

    详解Android 7.0 Settings 加载选项

    本篇文章主要介绍了Android 7.0 Settings 加载选项,Android 7.0 Settings顶部多了一个建议选项,多了个侧边栏,操作更加便捷了,有兴趣的可以了解一下。
    2017-02-02
  • Android创建悬浮窗的完整步骤

    Android创建悬浮窗的完整步骤

    这篇文章主要给大家介绍了关于Android创建悬浮窗的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • Android实现下载进度条效果

    Android实现下载进度条效果

    vivo商店在下载应用的时候,底部有一个圆角矩形的下载进度条,中间有一个进度文字,而且进度和文字交汇的时候,交汇部分的文字会从蓝色边为白色,会有一种一半白色字,一半蓝色字的效果。本文将仿照该样式实现一个
    2021-06-06
  • Android详细讲解谷歌推出的官方二维码扫描库

    Android详细讲解谷歌推出的官方二维码扫描库

    Google推出的官方二维码扫描库你知道吗?还不知道就落伍咯!本篇文字带你了解google二维码扫描库的详细情况与使用,还不知道的朋友快来看看吧
    2022-03-03
  • Android服务Service教程

    Android服务Service教程

    Android的服务是开发Android应用程序的重要组成部分。不同于活动Activity,服务是在后台运行,服务没有接口,生命周期也与活动Activity非常不同。通过使用服务我们可以实现一些后台操作,比如想从远程服务器加载一个网页等,下面来看看详细内容,需要的朋友可以参考下
    2021-11-11
  • Android判断当前栈顶Activity的包名代码示例

    Android判断当前栈顶Activity的包名代码示例

    这篇文章主要介绍了Android判断当前栈顶Activity的包名代码示例,分享了相关代码,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Android实战教程第四十篇之Chronometer实现倒计时

    Android实战教程第四十篇之Chronometer实现倒计时

    这篇文章主要介绍了Android实战教程第四十篇之Chronometer实现倒计时,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android实现完整游戏循环的方法

    Android实现完整游戏循环的方法

    这篇文章主要介绍了Android实现完整游戏循环的方法,以实例代码形式较为详细的分析了Android游戏循环的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android实现3种侧滑效果(仿qq侧滑、抽屉侧滑、普通侧滑)

    Android实现3种侧滑效果(仿qq侧滑、抽屉侧滑、普通侧滑)

    这篇文章主要为大家详细介绍了Android实现多种侧滑效果,包括仿qq侧滑,抽屉侧滑,普通侧滑三种效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • 详解AndroidStudio中代码重构菜单Refactor功能

    详解AndroidStudio中代码重构菜单Refactor功能

    这篇文章主要介绍了AndroidStudio中代码重构菜单Refactor功能详解,本文通过代码演示,功能截图来详细说明as为大名重构提供的各项功能,需要的朋友可以参考下
    2019-11-11

最新评论