修改申请权限的位置

This commit is contained in:
lizj 2021-05-06 17:37:00 +08:00
parent 49c0bc0d98
commit 8622e0a2b2

View File

@ -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
*