mirror of
https://gitee.com/shuto/cordova-plugin-network-information.git
synced 2025-02-23 12:12:48 +08:00
增加5g判断
This commit is contained in:
parent
eb6e88d0bb
commit
49c0bc0d98
@ -49,6 +49,7 @@ 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,19 +22,24 @@ 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;
|
||||
|
||||
@ -78,6 +83,7 @@ public class NetworkManager extends CordovaPlugin {
|
||||
public static final String TYPE_2G = "2g";
|
||||
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";
|
||||
@ -88,6 +94,10 @@ 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.
|
||||
@ -114,14 +124,37 @@ 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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
|
||||
pluginResult.setKeepCallback(true);
|
||||
callbackContext.sendPluginResult(pluginResult);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
this.connectionCallbackContext.sendPluginResult(pluginResult);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -262,6 +295,13 @@ 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);
|
||||
|
Loading…
Reference in New Issue
Block a user