换了种方式判断5g

This commit is contained in:
lizj 2021-05-07 10:45:43 +08:00
parent 8622e0a2b2
commit 373f05217f
2 changed files with 10 additions and 52 deletions

View File

@ -49,7 +49,6 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<config-file target="AndroidManifest.xml" parent="/*"> <config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</config-file> </config-file>
<source-file src="src/android/NetworkManager.java" target-dir="src/org/apache/cordova/networkinformation" /> <source-file src="src/android/NetworkManager.java" target-dir="src/org/apache/cordova/networkinformation" />

View File

@ -22,24 +22,19 @@ import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG; import org.apache.cordova.LOG;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import android.Manifest;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.Build; import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.Locale; 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_3G = "3g";
public static final String TYPE_4G = "4g"; public static final String TYPE_4G = "4g";
public static final String TYPE_5G = "5g"; public static final String TYPE_5G = "5g";
public static final String TYPE_NONE = "none"; public static final String TYPE_NONE = "none";
private static final String LOG_TAG = "NetworkManager"; private static final String LOG_TAG = "NetworkManager";
@ -94,10 +90,6 @@ public class NetworkManager extends CordovaPlugin {
BroadcastReceiver receiver; BroadcastReceiver receiver;
private String lastTypeOfNetwork; 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 * Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity. * get file paths associated with the Activity.
@ -124,20 +116,16 @@ 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;
this.getNetworkInfo(); NetworkInfo info = sockMan.getActiveNetworkInfo();
String connectionType = this.getTypeOfNetworkFallbackToTypeNoneIfNotConnected(info);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
return true; return true;
} }
return false; 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);
}
/** /**
* Stop network receiver. * Stop network receiver.
*/ */
@ -245,13 +233,7 @@ public class NetworkManager extends CordovaPlugin {
type = TYPE_NONE; type = TYPE_NONE;
} }
else { 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 { } else {
type = TYPE_NONE; type = TYPE_NONE;
@ -261,25 +243,6 @@ 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
* *
@ -301,13 +264,6 @@ public class NetworkManager extends CordovaPlugin {
* @return the type of mobile network we are on * @return the type of mobile network we are on
*/ */
private String getType(NetworkInfo info) { 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); String type = info.getTypeName().toLowerCase(Locale.US);
LOG.d(LOG_TAG, "toLower : " + type); LOG.d(LOG_TAG, "toLower : " + type);
@ -317,6 +273,7 @@ public class NetworkManager extends CordovaPlugin {
return TYPE_ETHERNET; return TYPE_ETHERNET;
} else if (type.equals(MOBILE) || type.equals(CELLULAR)) { } else if (type.equals(MOBILE) || type.equals(CELLULAR)) {
type = info.getSubtypeName().toLowerCase(Locale.US); type = info.getSubtypeName().toLowerCase(Locale.US);
int subType = info.getSubtype();
if (type.equals(GSM) || if (type.equals(GSM) ||
type.equals(GPRS) || type.equals(GPRS) ||
type.equals(EDGE) || type.equals(EDGE) ||
@ -336,6 +293,8 @@ public class NetworkManager extends CordovaPlugin {
type.equals(HSPA_PLUS) || type.equals(HSPA_PLUS) ||
type.equals(FOUR_G)) { type.equals(FOUR_G)) {
return TYPE_4G; return TYPE_4G;
} else if(subType == 20){ //api 29 时生效 为5G网络类型 值为20也是一样
return TYPE_5G;
} }
} }
return TYPE_UNKNOWN; return TYPE_UNKNOWN;