Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActiveNetworkInfo() is deprecated in API 29 [duplicate]

Tags:

android

I am using a code that check if user has internet active or not but after targeting sdk29 the functions bellow became deprecated

NetworkInfo

NetworkInfo.isConnected()

getActiveNetworkInfo()

Here is the code :

public static boolean isNetworkAvailable(Context context) {     if(context == null) { return false; }     ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);     // if no network is available networkInfo will be null, otherwise check if we are connected     try {         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();         if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {             Log.i("update_statut","Network is available : true");             return true;         }     } catch (Exception e) {         Log.i("update_statut",""+ e.getMessage());     }     Log.i("update_statut","Network is available : FALSE ");     return false; } 
like image 714
Chaouki Anass Avatar asked Jul 30 '19 18:07

Chaouki Anass


People also ask

Is getactivenetworkinfo and getallnetworkinfo deprecated?

getActiveNetworkInfo is deprecated on API 29. getAllNetworkInfo is deprecated on API 23. So, If you want to find the Network Connection status, you can use this code. you can see all NetworkCapability here. Show activity on this post. NetworkCapabilities is not deprecated in API 29 but it requires API 21 so I have called it on API 29 only.

Is this class deprecated at API level 29?

This class was deprecated in API level 29. Callers should instead use the ConnectivityManager.NetworkCallback API to learn about connectivity changes, or switch to use ConnectivityManager#getNetworkCapabilities or ConnectivityManager#getLinkProperties to get information synchronously.

Isonline() method deprecated in API 29?

unfortunately, that's deprecated in API 29. private fun isOnline (): Boolean { val connectivityManager = appContext.getSystemService (Context.CONNECTIVITY_SERVICE) as ConnectivityManager val networkInfo = connectivityManager.activeNetworkInfo return networkInfo != null && networkInfo.isConnected }

How to find the network connection status in API 23?

getAllNetworkInfo is deprecated on API 23. So, If you want to find the Network Connection status, you can use this code. you can see all NetworkCapability here.


2 Answers

It's deprecated base on Google Document

  • getActiveNetworkInfo is deprecated on API 29.
  • getAllNetworkInfo is deprecated on API 23.

So, If you want to find the Network Connection status, you can use this code.

kotlin :

private fun isNetworkAvailable(context: Context): Boolean {     val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {         val nw      = connectivityManager.activeNetwork ?: return false         val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false         return when {             actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true             actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true             //for other device how are able to connect with Ethernet             actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true             //for check internet over Bluetooth             actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true             else -> false         }     } else {         val nwInfo = connectivityManager.activeNetworkInfo ?: return false         return nwInfo.isConnected     } } 

Java :

private Boolean isNetworkAvailable(Application application) {     ConnectivityManager connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {         Network nw = connectivityManager.getActiveNetwork();         if (nw == null) return false;         NetworkCapabilities actNw = connectivityManager.getNetworkCapabilities(nw);         return actNw != null && (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) || actNw.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH));     } else {         NetworkInfo nwInfo = connectivityManager.getActiveNetworkInfo();         return nwInfo != null && nwInfo.isConnected();     } } 

you can see all NetworkCapability here.

like image 122
Ali shatergholi Avatar answered Oct 07 '22 04:10

Ali shatergholi


I have finally found a code that works on all APIs in case anybody want it

NetworkCapabilities is not deprecated in API 29 but it requires API 21 so I have called it on API 29 only.

However getActiveNetworkInfo() is deprecated only in API 29 and works on all APIs , so we can use it in all apis bellow 29

here's the code

    public static boolean isNetworkAvailable(Context context) {     if(context == null)  return false;       ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);      if (connectivityManager != null) {       if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {             NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());             if (capabilities != null) {                 if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {                     return true;                 } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {                     return true;                 }  else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)){                     return true;                 }             }         }      else {          try {             NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();             if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {                 Log.i("update_statut", "Network is available : true");                 return true;             }         } catch (Exception e) {             Log.i("update_statut", "" + e.getMessage());         }     } }     Log.i("update_statut","Network is available : FALSE ");     return false; } 
like image 40
Chaouki Anass Avatar answered Oct 07 '22 04:10

Chaouki Anass