android solve getNetworkInfo obsolete





Today, I will briefly talk about how to solve the outdated problem of getNetworkInfo in android.


Before, I wrote a blog on how to get whether the mobile phone is connected to the Internet. Some of the codes used the getNetworkInfo function of ConnectivityManager, but when I checked the code recently, I found that getNetworkInfo is out of date, so I searched for information on the Internet and found out about this not much. But there are still alternative functions to determine whether the phone is connected to the Internet. Record it here.


Describes the status of the current network Mobile and Wifi .

There are 2 inner classes NetworkInfo.DetailedState ( precise network state ) and NetworkInfo.State ( rough network state )

Detailed state

Coarse-grained state

IDLE

DISCONNECTED

SCANNING

CONNECTING

CONNECTING

CONNECTING

AUTHENTICATING

CONNECTING

CONNECTED

DISCONNECTING

DISCONNECTING

DISCONNECTED

DISCONNECTED

UNAVAILABLE

DISCONNECTED

FAILED

DISCONNECTED

 

Several commonly used functions

isAvailable()  isConnected()  getDetailedState()   getState()  getExtrInfo()

getType()  gets the type of the current network mobile or Wi-Fi

getTypeName() gets the type name of the current network "WIFI" or "MOBILE"

ConnectivityManager

It is used to query the network connection status and notify the application when the network status changes. By calling Context.getSystemService(Context.CONNECTIVITY_SERVICE) . The main function:

1.        Monitor network (Wi-Fi, GPRS, UMTS, etc) connections

2.        Send a broadcast when the network state changes

3.        Other networks that try to transfer when the network is lost

4.        Provide API

 Main function introduction

1.        NetworkInfo getActiveNetworkInfo()  to get the available network (available)

2.        NetworkInfo[] getAllNetworkInfo   gets all available networks (obsolete)

3.        NetworkInfo getNetworkInfo(int networkType) to get fixed network information (obsolete)

4.       isNetworkTypeValid(int networkType)


Android judge whether the current network status is connected function

Public static Boolean isNetworkConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context  
              .getApplicationContext().getSystemService(  
                     Context.CONNECTIVITY_SERVICE);  
        
       if (manager == null) {  
           return false;  
       }  
        
       NetworkInfo networkinfo = manager.getActiveNetworkInfo();  
        
       if (networkinfo == null || !networkinfo.isAvailable()) {  
           return false;  
       }  
   
       return true;  
    }


or this can also

/**
 * Is the network available
*
 * @param activity
 * @return
 */
 public static boolean isNetworkAvailable(Context context) {
 ConnectivityManager connectivity = (ConnectivityManager) context
 .getSystemService(Context.CONNECTIVITY_SERVICE);
   if (connectivity == null) {
      } else {
      NetworkInfo[] info = connectivity.getAllNetworkInfo();
      if (info != null) {
      for (int i = 0; i < info.length; i++) {
      if (info[i].getState() == NetworkInfo.State.CONNECTED) {
        return true;
           }
      }
    }
 }
   return false;
 }


More rigorous writing:

 
public static boolean checkNet(Context context) {

 try {
 ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 if (connectivity != null) {

 NetworkInfo info = connectivity.getActiveNetworkInfo();
 if (info != null && info.isConnected()) {

 if (info.getState() == NetworkInfo.State.CONNECTED) {
 return true;
 }
 }
 }
 } catch (Exception e) {
 return false;
}
 return false;
 }


To put it simply, in fact, ctiveNetworkInfo() is used to replace the outdated getNetworkInfo(int networkType), so that you can determine whether the mobile phone network is connected. If the network type is judged, what should we do?

/**
 * Determine whether the current network is a wifi network
* if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //判断3G网
*
 * @param context
 * @return boolean
 */
 public static boolean isWifi(Context context) {
 ConnectivityManager connectivityManager = (ConnectivityManager) context
 .getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
 if (activeNetInfo != null
 && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
 return true;
 }
 return false;
 }


/**
 * Determine whether the current network is a 3G network
*
 * @param context
 * @return boolean
 */
 public static boolean is3G(Context context) {
 ConnectivityManager connectivityManager = (ConnectivityManager) context
 .getSystemService(Context.CONNECTIVITY_SERVICE);
 NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
 if (activeNetInfo != null
 && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
 return true;
 }
 return false;
 }
}


The android solution to getNetworkInfo is outdated and finished.


It's that simple.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324873846&siteId=291194637