Android 獲取判斷是否有懸浮窗權(quán)限的方法
現(xiàn)在很多應(yīng)用都會(huì)用到懸浮窗,很多國(guó)產(chǎn)rom把懸浮窗權(quán)限加入控制了,你就需要判斷是否有懸浮窗權(quán)限,然后做對(duì)應(yīng)操作。
Android 原生有自帶權(quán)限管理的,只是被隱藏了??碼ndroid源碼在android.app下就有個(gè)AppOpsManager類(lèi)。
類(lèi)說(shuō)明如下:
/**
* 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>
*/
上面說(shuō)明了只對(duì)系統(tǒng)應(yīng)用有用,rom廠商們應(yīng)該就是利用這個(gè)AppOps機(jī)制開(kāi)放一些權(quán)限控制。
我們要判斷是否有權(quán)限該如何做呢?就只能通過(guò)反射去判斷了。
AppOpsManager的checkOp方法,就是檢測(cè)是否有某項(xiàng)權(quán)限的方法有這些返回值,分別是允許,忽略,錯(cuò)誤和默認(rèn):
/**
* 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才是確定有權(quán)限的。
類(lèi)里面checkOp方法如下,三個(gè)參數(shù)分別是操作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可以在該類(lèi)中找到靜態(tài)值定義,android23里面有62種權(quán)限,我們需要的是OP_SYSTEM_ALERT_WINDOW=24
知道這些就可以用反射把我們的方法寫(xiě)出了:
/**
* 判斷 懸浮窗口權(quán)限是否打開(kāi)
*
* @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;
}
測(cè)試在魅族華為小米大部分機(jī)型上都是可以的,但這個(gè)方法也不能保證正確,一些機(jī)型上會(huì)返回錯(cuò)誤即MODE_ERRORED,就是獲取不到權(quán)限值,這個(gè)方法就返回了false,但實(shí)際上懸浮窗是可以使用的。
以上這篇Android 獲取判斷是否有懸浮窗權(quán)限的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android實(shí)現(xiàn)桌面懸浮窗、蒙板效果實(shí)例代碼
- 不依賴于Activity的Android全局懸浮窗的實(shí)現(xiàn)
- Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié))
- Android應(yīng)用內(nèi)懸浮窗的實(shí)現(xiàn)方案示例
- Android實(shí)現(xiàn)類(lèi)似360,QQ管家那樣的懸浮窗
- Android實(shí)現(xiàn)類(lèi)似qq微信消息懸浮窗通知功能
- Android 8.0如何完美適配全局dialog懸浮窗彈出
- Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件的實(shí)例代碼
- android仿華為手機(jī)懸浮窗設(shè)計(jì)
相關(guān)文章
Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動(dòng)輪播的示例代碼
這篇文章主要介紹了Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動(dòng)輪播的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Android中實(shí)現(xiàn)在矩形框中輸入文字顯示剩余字?jǐn)?shù)的功能
在矩形輸入框框中輸入文字顯示剩余字?jǐn)?shù)的功能在app開(kāi)發(fā)中經(jīng)常會(huì)見(jiàn)到,今天小編就通過(guò)實(shí)例代碼給大家分享android實(shí)現(xiàn)輸入框提示剩余字?jǐn)?shù)功能,代碼簡(jiǎn)單易懂,需要的朋友參考下吧2017-04-04
Android實(shí)現(xiàn)授權(quán)訪問(wèn)網(wǎng)頁(yè)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)授權(quán)訪問(wèn)網(wǎng)頁(yè)的方法,需要的朋友可以參考下2014-07-07
Android 仿余額寶數(shù)字跳動(dòng)動(dòng)畫(huà)效果完整代碼
這篇文章主要介紹了Android 仿余額寶數(shù)字跳動(dòng)動(dòng)畫(huà)效果完整代碼,需要的朋友可以參考下2017-11-11
Android UI設(shè)計(jì)與開(kāi)發(fā)之PopupWindow仿騰訊新聞底部彈出菜單
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開(kāi)發(fā)之PopupWindow仿騰訊新聞底部彈出菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android開(kāi)發(fā)中怎樣調(diào)用系統(tǒng)Email發(fā)送郵件(多種調(diào)用方式)
在Android中調(diào)用其他程序進(jìn)行相關(guān)處理,幾乎都是使用的Intent,所以,Email也不例外,所謂的調(diào)用Email,只是說(shuō)Email可以接收Intent并做這些事情2013-06-06

