Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getAllNetworkInfo() is Deprecated. What is the alternative?

I want to use the ConnectivityManager which provides the getAllNetworkInfo() method for checking the availability of network in Android. This method was deprecated in API level 23. And the developer documentation is suggesting to use getAllNetworks() instead. I tried but couldn't get the exact functionalities which I was getting out of my old code. Please someone could guide me how to use the getAllNetworks() method?

 public boolean isConnectingToInternet(){     ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);       if (connectivity != null)        {           @SuppressWarnings("deprecation")         NetworkInfo[] info = connectivity.getAllNetworkInfo();           //use getAllNetworks() instead           if (info != null)                for (int i = 0; i < info.length; i++)                    if (info[i].getState() == NetworkInfo.State.CONNECTED)                   {                       return true;                   }       }       return false; } 
like image 295
Anees U Avatar asked Aug 27 '15 06:08

Anees U


People also ask

What is ConnectivityManager in Android?

android.net.ConnectivityManager. Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. The primary responsibilities of this class are to: Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)

How can you instantiate the object of Connectivity Manager?

Checking Network Connection getSystemService(Context. CONNECTIVITY_SERVICE); Once you instantiate the object of ConnectivityManager class, you can use getAllNetworkInfo method to get the information of all the networks. This method returns an array of NetworkInfo.

What is network watchlist?

The watchlist is a collection of endpoint pairs. Each endpoint pair has a vantage point at one end and a market city and provider at the other end. The watchlist updates latency measurements for each endpoint pair daily. Use the watchlist to identify performance trends for your locations and providers of interest.


1 Answers

When i update my deprecated code and still want to support backward Api. i use this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.WANTED API VERSION){ //code }else{ //code } 

In this way each device use the appropriate code for it. Example:

public class ConnectionDetector {      private Context mContext;      public ConnectionDetector(Context context) {         this.mContext = context;     }     /**      * Checking for all possible internet providers      * **/     public boolean isConnectingToInternet() {         ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {             Network[] networks = connectivityManager.getAllNetworks();             NetworkInfo networkInfo;             for (Network mNetwork : networks) {                 networkInfo = connectivityManager.getNetworkInfo(mNetwork);                 if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {                     return true;                 }             }         }else {             if (connectivityManager != null) {                 //noinspection deprecation                 NetworkInfo[] info = connectivityManager.getAllNetworkInfo();                 if (info != null) {                     for (NetworkInfo anInfo : info) {                         if (anInfo.getState() == NetworkInfo.State.CONNECTED) {                             LogUtils.d("Network",                                     "NETWORKNAME: " + anInfo.getTypeName());                             return true;                         }                     }                 }             }         }         Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show();         return false;     } } 
like image 114
Maor Hadad Avatar answered Sep 18 '22 13:09

Maor Hadad