From 373f05217f79cd85cc7d2e0011e3993037136d77 Mon Sep 17 00:00:00 2001 From: lizj Date: Fri, 7 May 2021 10:45:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=A2=E4=BA=86=E7=A7=8D=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=88=A4=E6=96=AD5g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin.xml | 1 - src/android/NetworkManager.java | 61 ++++++--------------------------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/plugin.xml b/plugin.xml index 6ec6dde..a05111f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -49,7 +49,6 @@ xmlns:android="http://schemas.android.com/apk/res/android" - diff --git a/src/android/NetworkManager.java b/src/android/NetworkManager.java index 4823128..d527277 100755 --- a/src/android/NetworkManager.java +++ b/src/android/NetworkManager.java @@ -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,20 +116,16 @@ public class NetworkManager extends CordovaPlugin { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getConnectionInfo")) { 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 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. */ @@ -245,13 +233,7 @@ 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); - } + 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;