關於懸浮窗Settings.canDrawOverlays獲取許可權是否開通出錯
阿新 • • 發佈:2018-12-21
申請許可權:
private static final int PermissionRequestCode = 10;
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PermissionRequestCode);
獲取是否開通:
Handler mHandler; @Override protected void onActivityResult(final int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (mHandler == null){ mHandler = new Handler(Looper.getMainLooper()); } mHandler.postDelayed(new Runnable() { @Override public void run() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requestCode == PermissionRequestCode){ boolean nOpen = Settings.canDrawOverlays(XXXActivity.this); if (nOpen){ //開啟 }else { //關閉 } Log.e("PermissionRequest", "open = " + nOpen); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requestCode == PermissionRequestCode){ AppOpsManager appOpsMgr = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window", android.os.Process.myUid(), getPackageName()); if (mode == 1 || mode == 0){ //許可權已開起 }else if (mode == 2){ //許可權已關閉 } Log.e("PermissionRequest", "mode = " + mode); //此方法不可行,親測華為8.0手機 未申請狀態時 也返回1 } } }, 500); }
表示不理解,反正延時0.5秒再獲取,就對了。
直接獲取的話,得到的結果是仍然沒有開通許可權。