這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在Android中判斷是否有懸浮窗權(quán)限,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司主營(yíng)南木林網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,南木林h5小程序開(kāi)發(fā)搭建,南木林網(wǎng)站營(yíng)銷推廣歡迎南木林等地區(qū)企業(yè)咨詢
Android 原生有自帶權(quán)限管理的,只是被隱藏了??碼ndroid源碼在android.app下就有個(gè)AppOpsManager類。
類說(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)限的。
類里面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可以在該類中找到靜態(tài)值定義,android23里面有62種權(quán)限,我們需要的是OP_SYSTEM_ALERT_WINDOW=24
知道這些就可以用反射把我們的方法寫出了:
/** * 判斷 懸浮窗口權(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; }
上述就是小編為大家分享的怎么在Android中判斷是否有懸浮窗權(quán)限了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:怎么在Android中判斷是否有懸浮窗權(quán)限
文章路徑:http://jinyejixie.com/article14/psgdde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、域名注冊(cè)、網(wǎng)站改版、云服務(wù)器、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)