日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2025/3/12 Android 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android悬浮动态权限,Android 获取判断是否有悬浮窗权限的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在很多應用都會用到懸浮窗,很多國產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 获取判断是否有悬浮窗权限的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。