mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-31 10:35:43 +08:00
修改申请权限的位置
This commit is contained in:
parent
49c0bc0d98
commit
8622e0a2b2
@ -124,31 +124,12 @@ public class NetworkManager extends CordovaPlugin {
|
|||||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
||||||
if (action.equals("getConnectionInfo")) {
|
if (action.equals("getConnectionInfo")) {
|
||||||
this.connectionCallbackContext = callbackContext;
|
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();
|
this.getNetworkInfo();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
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(){
|
void getNetworkInfo(){
|
||||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||||
String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
|
String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
|
||||||
@ -264,8 +245,14 @@ public class NetworkManager extends CordovaPlugin {
|
|||||||
type = TYPE_NONE;
|
type = TYPE_NONE;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
// 申请权限
|
||||||
|
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);
|
type = getType(info);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
type = TYPE_NONE;
|
type = TYPE_NONE;
|
||||||
}
|
}
|
||||||
@ -274,6 +261,25 @@ public class NetworkManager extends CordovaPlugin {
|
|||||||
return type;
|
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
|
* Create a new plugin result and send it back to JavaScript
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user