답변:
문제가 전화의 네트워크가 연결되어 있고 요구 사항을 충족 할만큼 충분히 빠른지 확인하는 경우에서 반환 한 모든 네트워크 유형을 처리해야합니다 getSubType()
.
정확히이 작업을 수행하기 위해이 수업을 연구하고 작성하는 데 1 ~ 2 시간이 걸렸으며, 유용하다고 생각되는 다른 사람들과 공유 할 것이라고 생각했습니다.
다음은 수업 의 요점 이므로 포크하여 편집 할 수 있습니다.
package com.emil.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
/**
* Check device's network connectivity and speed
* @author emil http://stackoverflow.com/users/220710/emil
*
*/
public class Connectivity {
/**
* Get the network info
* @param context
* @return
*/
public static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* Check if there is any connectivity
* @param context
* @return
*/
public static boolean isConnected(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected());
}
/**
* Check if there is any connectivity to a Wifi network
* @param context
* @return
*/
public static boolean isConnectedWifi(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
/**
* Check if there is any connectivity to a mobile network
* @param context
* @return
*/
public static boolean isConnectedMobile(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
/**
* Check if there is fast connectivity
* @param context
* @return
*/
public static boolean isConnectedFast(Context context){
NetworkInfo info = Connectivity.getNetworkInfo(context);
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(),info.getSubtype()));
}
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
*/
public static boolean isConnectionFast(int type, int subType){
if(type==ConnectivityManager.TYPE_WIFI){
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}else{
return false;
}
}
}
또한이 권한을 AndroidManifest.xml에 추가하십시오
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
네트워크 속도의 소스는 wikipedia & http://3gstore.com/page/78_what_is_ev__do_mobile_broadband.html을 포함합니다.
targetSdkVersion
설정 사용 방법을 몰랐습니다 . 해킹을 제거하기 위해 게시물을 편집했습니다. 감사합니다.
연결 유형에 대한보다 정확하고 사용자 친화적 인 정보를 얻습니다. 이 코드를 사용할 수 있습니다 ( TelephonyManager.java 의 @hide 메소드에서 파생 됨 ).
이 메소드는 현재 연결 유형을 설명하는 문자열을 리턴합니다.
즉 "WIFI", "2G", "3G", "4G", "5G", "-"(연결되지 않음) 또는 "?"중 하나입니다. (알 수 없는)
비고 :이 코드에는 API 25+가 필요하지만 const 대신 int를 사용하여 이전 버전을 쉽게 지원할 수 있습니다. (코드 주석 참조).
public static String getNetworkClass(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null || !info.isConnected())
return "-"; // not connected
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return "WIFI";
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkType = info.getSubtype();
switch (networkType) {
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
case TelephonyManager.NETWORK_TYPE_GSM: // api<25: replace by 16
return "2G";
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 12
case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11: replace by 14
case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13: replace by 15
case TelephonyManager.NETWORK_TYPE_TD_SCDMA: // api<25: replace by 17
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE: // api<11: replace by 13
case TelephonyManager.NETWORK_TYPE_IWLAN: // api<25: replace by 18
case 19: // LTE_CA
return "4G";
case TelephonyManager.NETWORK_TYPE_NR: // api<29: replace by 20
return "5G";
default:
return "?";
}
}
return "?";
}
자세한 내용은 getSubtype ()을 사용할 수 있습니다. 여기에서 슬라이드 9를 확인하십시오. http://dl.google.com/io/2009/pres/W_0300_CodingforLife-BatteryLifeThatIs.pdf
ConnectivityManager mConnectivity = null;
TelephonyManager mTelephony = null;
// Skip if no connection, or background data disabled
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return false;
}
// Only update if WiFi or 3G is connected and not roaming
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE
&& netSubtype == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
또한 이에 대한 자세한 내용은 Emil의 답변을 확인하십시오.
위의 @Emil의 답변은 훌륭합니다.
소규모 추가 : TelephonyManager를 사용하여 네트워크 유형을 감지하는 것이 이상적입니다. 따라서 위의 내용을 대신 읽어야합니다.
/**
* Check if there is fast connectivity
* @param context
* @return
*/
public static boolean isConnectedFast(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return (info != null && info.isConnected() && Connectivity.isConnectionFast(info.getType(), tm.getNetworkType()));
}
Emil Davtyan의 답변은 훌륭하지만 그의 답변에서 설명되지 않은 네트워크 유형이 추가되었습니다. 따라서 isConnectionFast(int type, int subType)
true 여야하는 경우 false를 리턴 할 수 있습니다.
다음 API에서 리플렉션을 사용하여 추가 된 네트워크 유형을 설명하는 수정 된 클래스는 다음과 같습니다.
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* <p>Utility methods to check the current network connection status.</p>
*
* <p>This requires the caller to hold the permission
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.</p>
*/
public class NetworkUtils {
/** The absence of a connection type. */
public static final int TYPE_NONE = -1;
/** Unknown network class. */
public static final int NETWORK_CLASS_UNKNOWN = 0;
/** Class of broadly defined "2G" networks. */
public static final int NETWORK_CLASS_2_G = 1;
/** Class of broadly defined "3G" networks. */
public static final int NETWORK_CLASS_3_G = 2;
/** Class of broadly defined "4G" networks. */
public static final int NETWORK_CLASS_4_G = 3;
/**
* Returns details about the currently active default data network. When connected, this network
* is the default route for outgoing connections. You should always check {@link
* NetworkInfo#isConnected()} before initiating network traffic. This may return {@code null}
* when there is no default network.
*
* @return a {@link NetworkInfo} object for the current default network or {@code null} if no
* network default network is currently active
*
* This method requires the call to hold the permission
* {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
* @see ConnectivityManager#getActiveNetworkInfo()
*/
public static NetworkInfo getInfo(Context context) {
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
}
/**
* Reports the current network type.
*
* @return {@link ConnectivityManager#TYPE_MOBILE}, {@link ConnectivityManager#TYPE_WIFI} ,
* {@link ConnectivityManager#TYPE_WIMAX}, {@link ConnectivityManager#TYPE_ETHERNET}, {@link
* ConnectivityManager#TYPE_BLUETOOTH}, or other types defined by {@link ConnectivityManager}.
* If there is no network connection then -1 is returned.
* @see NetworkInfo#getType()
*/
public static int getType(Context context) {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return TYPE_NONE;
}
return info.getType();
}
/**
* Return a network-type-specific integer describing the subtype of the network.
*
* @return the network subtype
* @see NetworkInfo#getSubtype()
*/
public static int getSubType(Context context) {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return TYPE_NONE;
}
return info.getSubtype();
}
/** Returns the NETWORK_TYPE_xxxx for current data connection. */
public static int getNetworkType(Context context) {
return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
.getNetworkType();
}
/** Check if there is any connectivity */
public static boolean isConnected(Context context) {
return getType(context) != TYPE_NONE;
}
/** Check if there is any connectivity to a Wifi network */
public static boolean isWifiConnection(Context context) {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return false;
}
switch (info.getType()) {
case ConnectivityManager.TYPE_WIFI:
return true;
default:
return false;
}
}
/** Check if there is any connectivity to a mobile network */
public static boolean isMobileConnection(Context context) {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return false;
}
switch (info.getType()) {
case ConnectivityManager.TYPE_MOBILE:
return true;
default:
return false;
}
}
/** Check if the current connection is fast. */
public static boolean isConnectionFast(Context context) {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return false;
}
switch (info.getType()) {
case ConnectivityManager.TYPE_WIFI:
case ConnectivityManager.TYPE_ETHERNET:
return true;
case ConnectivityManager.TYPE_MOBILE:
int networkClass = getNetworkClass(getNetworkType(context));
switch (networkClass) {
case NETWORK_CLASS_UNKNOWN:
case NETWORK_CLASS_2_G:
return false;
case NETWORK_CLASS_3_G:
case NETWORK_CLASS_4_G:
return true;
}
default:
return false;
}
}
private static int getNetworkClassReflect(int networkType)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method getNetworkClass = TelephonyManager.class.getDeclaredMethod("getNetworkClass", int.class);
if (!getNetworkClass.isAccessible()) {
getNetworkClass.setAccessible(true);
}
return (int) getNetworkClass.invoke(null, networkType);
}
/**
* Return general class of network type, such as "3G" or "4G". In cases where classification is
* contentious, this method is conservative.
*/
public static int getNetworkClass(int networkType) {
try {
return getNetworkClassReflect(networkType);
} catch (Exception ignored) {
}
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case 16: // TelephonyManager.NETWORK_TYPE_GSM:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORK_CLASS_2_G;
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:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return NETWORK_CLASS_3_G;
case TelephonyManager.NETWORK_TYPE_LTE:
case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
return NETWORK_CLASS_4_G;
default:
return NETWORK_CLASS_UNKNOWN;
}
}
private NetworkUtils() {
throw new AssertionError();
}
}
if (NetworkUtils.isWifiConnection(this) { /* do stuff */ }
이 작업을 수행하기 위해 사용자 지정 방법을 만들 수 있습니다.
public String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
switch (networkType) {
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:
return "2G";
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:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE:
return "4G";
default:
return "Unknown";
}
}
Emil의 끔찍한 답변 외에도 휴대 전화에서 데이터를 끌 수 있으므로 실제로 인터넷에 액세스 할 수 있는지 확인하는 방법을 하나 더 추가하고 싶습니다.
public static boolean hasInternetAccess(Context c){
TelephonyManager tm = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
if(isConnected(c) && tm.getDataState() == TelephonyManager.DATA_CONNECTED)
return true;
else
return false;
}
이는 셀룰러 데이터 연결이 있는지 확인하기위한 것이며 WiFi가 연결되면 셀룰러 데이터가 꺼져 있으므로 WiFi가 연결되어 있으면 false를 반환합니다.
이처럼 확인할 수 있습니다
public void checktype() {
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
}
}
}
현재는 MOBILE 및 WIFI 만 지원됩니다. 인간이 읽을 수있는 유형 함수를 살펴보십시오 .
당신은 이것을 시도 할 수 있습니다 :
public String ConnectionQuality() {
NetworkInfo info = getInfo(context);
if (info == null || !info.isConnected()) {
return "UNKNOWN";
}
if(info.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int numberOfLevels = 5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
if(level == 2 )
return "POOR";
else if(level == 3 )
return "MODERATE";
else if(level == 4 )
return "GOOD";
else if(level == 5 )
return "EXCELLENT";
else
return "UNKNOWN";
}else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkClass = getNetworkClass(getNetworkType(context));
if(networkClass == 1)
return "POOR";
else if(networkClass == 2 )
return "GOOD";
else if(networkClass == 3 )
return "EXCELLENT";
else
return "UNKNOWN";
}else
return "UNKNOWN";
}
public NetworkInfo getInfo(Context context) {
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
}
public int getNetworkClass(int networkType) {
try {
return getNetworkClassReflect(networkType);
}catch (Exception ignored) {
}
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_GPRS:
case 16: // TelephonyManager.NETWORK_TYPE_GSM:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return 1;
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:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
case 17: // TelephonyManager.NETWORK_TYPE_TD_SCDMA:
return 2;
case TelephonyManager.NETWORK_TYPE_LTE:
case 18: // TelephonyManager.NETWORK_TYPE_IWLAN:
return 3;
default:
return 0;
}
}
private int getNetworkClassReflect(int networkType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method getNetworkClass = TelephonyManager.class.getDeclaredMethod("getNetworkClass", int.class);
if (!getNetworkClass.isAccessible()) {
getNetworkClass.setAccessible(true);
}
return (Integer) getNetworkClass.invoke(null, networkType);
}
public static int getNetworkType(Context context) {
return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType();
}
어떤 유형의 네트워크를 감지하고 부울 값이 연결되어 있는지 또는 스 니펫 아래에서 사용하지 않는지 확인하십시오.
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
public class NetworkManagerUtils {
/**
* Get the network info
* @param context
* @return
*/
public static NetworkInfo getNetworkInfo(Context context){
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo();
}
/**
* Check if there is any connectivity
* @param context
* @return
*/
public static boolean isConnected(Context context){
NetworkInfo info = NetworkManagerUtils.getNetworkInfo(context);
return (info != null && info.isConnected());
}
/**
* Check if there is any connectivity to a Wifi network
* @param context.
* @param type
* @return
*/
public static boolean isConnectedWifi(Context context){
NetworkInfo info = NetworkManagerUtils.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
/**
* Check if there is any connectivity to a mobile network
* @param context
* @param type
* @return
*/
public static boolean isConnectedMobile(Context context){
NetworkInfo info = NetworkManagerUtils.getNetworkInfo(context);
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
/**
* Check if there is fast connectivity
* @param context
* @return
*/
public static boolean isConnectedFast(Context context){
NetworkInfo info = NetworkManagerUtils.getNetworkInfo(context);
return (info != null && info.isConnected() && NetworkManagerUtils.isConnectionFast(info.getType(),info.getSubtype()));
}
/**
* Check if the connection is fast
* @param type
* @param subType
* @return
*/
public static boolean isConnectionFast(int type, int subType){
if(type== ConnectivityManager.TYPE_WIFI){
return true;
}else if(type==ConnectivityManager.TYPE_MOBILE){
switch(subType){
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return true; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return false;
}
}else{
return false;
}
}
public static String getNetworkClass(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info == null || !info.isConnected())
return "-"; // not connected
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return "WIFI";
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
int networkType = info.getSubtype();
switch (networkType) {
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
case TelephonyManager.NETWORK_TYPE_GSM: // api<25: replace by 16
return "2G";
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 12
case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11: replace by 14
case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13: replace by 15
case TelephonyManager.NETWORK_TYPE_TD_SCDMA: // api<25: replace by 17
return "3G";
case TelephonyManager.NETWORK_TYPE_LTE: // api<11: replace by 13
case TelephonyManager.NETWORK_TYPE_IWLAN: // api<25: replace by 18
case 19: // LTE_CA
return "4G";
default:
return "?";
}
}
return "?";
}
}
다음 클래스를 사용하여 네트워크 유형, 고속 네트워크 등과 같은 네트워크 상태를 얻는 컨텍스트를 전달하십시오.
여러 가지 방법으로 아래에 표시되어 있습니다. ConnectivityManager 클래스에는 많은 네트워크 유형이 있습니다. 또한 API> = 21 인 경우 NetworkCapabilities 클래스에서 네트워크 유형을 확인할 수 있습니다.
ConnectivityMonitor connectivityMonitor = ConnectivityMonitor.getInstance(this);
boolean isWiFiConnected = connectivityMonitor.isWifiConnection();
boolean isMobileConnected = connectivityMonitor.isConnected(ConnectivityManager.TYPE_MOBILE);
Log.e(TAG, "onCreate: isWiFiConnected " + isWiFiConnected);
Log.e(TAG, "onCreate: isMobileConnected " + isMobileConnected);
ConnectivityMonitor.Listener connectivityListener = new ConnectivityMonitor.Listener() {
@Override
public void onConnectivityChanged(boolean connected, @Nullable NetworkInfo networkInfo) {
Log.e(TAG, "onConnectivityChanged: connected " + connected);
Log.e(TAG, "onConnectivityChanged: networkInfo " + networkInfo);
if (networkInfo != null) {
boolean isWiFiConnected = networkInfo.getType() == NetworkCapabilities.TRANSPORT_WIFI;
boolean isMobileConnected = networkInfo.getType() == NetworkCapabilities.TRANSPORT_CELLULAR;
Log.e(TAG, "onConnectivityChanged: isWiFiConnected " + isWiFiConnected);
Log.e(TAG, "onConnectivityChanged: isMobileConnected " + isMobileConnected);
}
}
};
connectivityMonitor.addListener(connectivityListener);
public static final int
입니다. 따라서 "해킹"을 수행 할 필요가 없습니다. 최신 SDK를 대상으로하는 컴파일러는 인스턴스가 아닌 실제 값 (정수)을 바이트 코드로 컴파일합니다.