From 8622e0a2b2e16c8ab67f8e5a7040fc952857033d Mon Sep 17 00:00:00 2001 From: lizj Date: Thu, 6 May 2021 17:37:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=B3=E8=AF=B7=E6=9D=83?= =?UTF-8?q?=E9=99=90=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/android/NetworkManager.java | 50 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index bb15862..4823128 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -124,31 +124,12 @@ public class NetworkManager extends CordovaPlugin { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getConnectionInfo")) { this.connectionCallbackContext = callbackContext; - if(!PermissionHelper.hasPermission(this, Manifest.permission.READ_PHONE_STATE)) { - PermissionHelper.requestPermission(this, READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE); - } else { - this.getNetworkInfo(); - return true; - } + this.getNetworkInfo(); + return true; } return false; } - public void onRequestPermissionResult(int requestCode, String[] permissions, - int[] grantResults) throws JSONException { - for (int r : grantResults) { - if (r == PackageManager.PERMISSION_DENIED) { - this.connectionCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); - return; - } - } - switch (requestCode) { - case READ_PHONE_STATE: - this.getNetworkInfo(); - break; - } - } - void getNetworkInfo(){ NetworkInfo info = sockMan.getActiveNetworkInfo(); String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info); @@ -264,7 +245,13 @@ public class NetworkManager extends CordovaPlugin { type = TYPE_NONE; } else { - type = getType(info); + // 申请权限 + if (!PermissionHelper.hasPermission(this, Manifest.permission.READ_PHONE_STATE)) { + PermissionHelper.requestPermission(this, READ_PHONE_STATE, Manifest.permission.READ_PHONE_STATE); + return ""; + } else { + type = getType(info); + } } } else { type = TYPE_NONE; @@ -274,6 +261,25 @@ public class NetworkManager extends CordovaPlugin { return type; } + public void onRequestPermissionResult(int requestCode, String[] permissions, + int[] grantResults) throws JSONException { + if (grantResults.length==0){ + return; + } + for (int r : grantResults) { + if (r == PackageManager.PERMISSION_DENIED) { + this.connectionCallbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, PERMISSION_DENIED_ERROR)); + return; + } + } + switch (requestCode) { + case READ_PHONE_STATE: + NetworkInfo info = sockMan.getActiveNetworkInfo(); + this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info); + break; + } + } + /** * Create a new plugin result and send it back to JavaScript *