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 *