version:4.3

fix:优化
update:
This commit is contained in:
2022-04-19 20:59:38 +08:00
parent 69a8934bd4
commit caa8608f52
3 changed files with 51 additions and 6 deletions

View File

@@ -6,12 +6,13 @@ import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;
public class NetworkUtils {
public static boolean isConnected(Context context) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= 23) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//获取网络属性
NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(mConnectivityManager.getActiveNetwork());
if (networkCapabilities != null) {
@@ -26,4 +27,47 @@ public class NetworkUtils {
}
return false;
}
public String getNetworkType(Context context) {
String strNetworkType = "UnKnown";
final NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.getType() == 1) {
strNetworkType = "WIFI";
} else if (activeNetworkInfo != null && activeNetworkInfo.getType() == 0) {
String subtypeName = activeNetworkInfo.getSubtypeName();
switch (((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType()) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
strNetworkType = "2G";
break;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12
case TelephonyManager.NETWORK_TYPE_HSPAP: //api<13 : replace by 15
strNetworkType = "3G";
break;
case TelephonyManager.NETWORK_TYPE_LTE:
strNetworkType = "4G";
break;
default:
if ("TD-SCDMA".equalsIgnoreCase(subtypeName) || "WCDMA".equalsIgnoreCase(subtypeName) || "CDMA2000".equalsIgnoreCase(subtypeName)) {
strNetworkType = "3G";
break;
}
strNetworkType = subtypeName;
break;
}
}
return strNetworkType;
}
}

View File

@@ -284,7 +284,7 @@ public class TimeUtils {
long now = mNtpClient.getNtpTime();//now就是获取的时间
return now;
} else {
Log.e("getTimeFromNtpServer", " failed");
Log.e("getTimeFromNtpServer", " failed");
}
return -1;
}