mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-01-18 13:42:49 +08:00
换了种方式判断5g
This commit is contained in:
parent
8622e0a2b2
commit
373f05217f
@ -49,7 +49,6 @@ xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
|
||||
<config-file target="AndroidManifest.xml" parent="/*">
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
</config-file>
|
||||
|
||||
<source-file src="src/android/NetworkManager.java" target-dir="src/org/apache/cordova/networkinformation" />
|
||||
|
@ -22,24 +22,19 @@ import org.apache.cordova.CallbackContext;
|
||||
import org.apache.cordova.CordovaInterface;
|
||||
import org.apache.cordova.CordovaPlugin;
|
||||
import org.apache.cordova.LOG;
|
||||
import org.apache.cordova.PermissionHelper;
|
||||
import org.apache.cordova.PluginResult;
|
||||
import org.apache.cordova.CordovaWebView;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@ -84,6 +79,7 @@ public class NetworkManager extends CordovaPlugin {
|
||||
public static final String TYPE_3G = "3g";
|
||||
public static final String TYPE_4G = "4g";
|
||||
public static final String TYPE_5G = "5g";
|
||||
|
||||
public static final String TYPE_NONE = "none";
|
||||
|
||||
private static final String LOG_TAG = "NetworkManager";
|
||||
@ -94,10 +90,6 @@ public class NetworkManager extends CordovaPlugin {
|
||||
BroadcastReceiver receiver;
|
||||
private String lastTypeOfNetwork;
|
||||
|
||||
// 权限
|
||||
public static final int READ_PHONE_STATE = 1;
|
||||
public static final int PERMISSION_DENIED_ERROR = 20;
|
||||
|
||||
/**
|
||||
* Sets the context of the Command. This can then be used to do things like
|
||||
* get file paths associated with the Activity.
|
||||
@ -124,18 +116,14 @@ public class NetworkManager extends CordovaPlugin {
|
||||
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
|
||||
if (action.equals("getConnectionInfo")) {
|
||||
this.connectionCallbackContext = callbackContext;
|
||||
this.getNetworkInfo();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void getNetworkInfo(){
|
||||
NetworkInfo info = sockMan.getActiveNetworkInfo();
|
||||
String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
|
||||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
|
||||
pluginResult.setKeepCallback(true);
|
||||
this.connectionCallbackContext.sendPluginResult(pluginResult);
|
||||
callbackContext.sendPluginResult(pluginResult);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,14 +233,8 @@ public class NetworkManager extends CordovaPlugin {
|
||||
type = TYPE_NONE;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
type = TYPE_NONE;
|
||||
}
|
||||
@ -261,25 +243,6 @@ 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
|
||||
*
|
||||
@ -301,13 +264,6 @@ public class NetworkManager extends CordovaPlugin {
|
||||
* @return the type of mobile network we are on
|
||||
*/
|
||||
private String getType(NetworkInfo info) {
|
||||
// 5g单独处理
|
||||
TelephonyManager telephonyManager = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
|
||||
int networkType = telephonyManager.getNetworkType();
|
||||
Log.d(LOG_TAG, "getType: " + networkType);
|
||||
if (networkType == 20) {// 对应的20 只有依赖为android 10.0才有此属性
|
||||
return TYPE_5G;
|
||||
}
|
||||
String type = info.getTypeName().toLowerCase(Locale.US);
|
||||
|
||||
LOG.d(LOG_TAG, "toLower : " + type);
|
||||
@ -317,6 +273,7 @@ public class NetworkManager extends CordovaPlugin {
|
||||
return TYPE_ETHERNET;
|
||||
} else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
|
||||
type = info.getSubtypeName().toLowerCase(Locale.US);
|
||||
int subType = info.getSubtype();
|
||||
if (type.equals(GSM) ||
|
||||
type.equals(GPRS) ||
|
||||
type.equals(EDGE) ||
|
||||
@ -336,6 +293,8 @@ public class NetworkManager extends CordovaPlugin {
|
||||
type.equals(HSPA_PLUS) ||
|
||||
type.equals(FOUR_G)) {
|
||||
return TYPE_4G;
|
||||
} else if(subType == 20){ //api 29 时生效 为5G网络类型 值为20也是一样
|
||||
return TYPE_5G;
|
||||
}
|
||||
}
|
||||
return TYPE_UNKNOWN;
|
||||
|
Loading…
Reference in New Issue
Block a user