69078

ConnectivityManager.getActiveNetworkInfo() / NetworkInfo is deprecated in API 29. What's an alt

<h3>Question</h3>

Seem like whole NetworkInfo is deprecated on API 29.

So I am looking for an alternative to check if the network is connected. E.g. alternative to

connectivityManager.activeNetworkInfo?.isConnected == true

Sidenote: I know there is a callback now, but I'd like to get this info synchronously. Also, of course I'm aware that it may not be a precise info though I'd like to have it.


<h3>Answer1:</h3>

The solution is this:

val capability = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) return capability?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?: false
<h3>Answer2:</h3>

This is working on API 29:

connectivityManager.isDefaultNetworkActive
<h3>Answer3:</h3>
android.net.NetworkInfo

This class was deprecated in API level 29. You should instead use the ConnectivityManager.NetworkCallback API to know about connectivity changes

Referrence Link https://developer.android.com/reference/android/net/NetworkInfo.html

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest.Builder builder = new NetworkRequest.Builder(); connectivityManager.registerNetworkCallback( builder.build(), new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { // Network Available } @Override public void onLost(Network network) { // Network Not Available } } );
<h3>Answer4:</h3>

The solution is

public void registerNetworkCallback() { try { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkRequest.Builder builder = new NetworkRequest.Builder(); connectivityManager.registerNetworkCallback(builder.build(),new ConnectivityManager.NetworkCallback() { @Override public void onAvailable(Network network) { Variables.isNetworkConnected = true; // Global Static Variable } @Override public void onLost(Network network) { Variables.isNetworkConnected = false; // Global Static Variable } } ); Variables.isNetworkConnected = false; }catch (Exception e){ Variables.isNetworkConnected = false; } }

Check full code here : Gist


<h3>Answer5:</h3>
private boolean checkInternetConnection(Activity activity) { ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { Network network = null; if (connectivityManager == null) { return false; } else { network = connectivityManager.getActiveNetwork(); NetworkCapabilities networkCapabilities = connectivityManager.getNetworkCapabilities(network); if (networkCapabilities == null) { return false; } if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { return true; } } } else { if (connectivityManager == null) { return false; } if (connectivityManager.getActiveNetworkInfo() == null) { return false; } return connectivityManager.getActiveNetworkInfo().isConnected(); } return false; }

来源:https://stackoverflow.com/questions/56984016/connectivitymanager-getactivenetworkinfo-networkinfo-is-deprecated-in-api-29

Recommend

  • Upload document to specific folder in a SharePoint Document library using WebClient
  • How to use Substring in SSIS
  • Thumbs system on Urban Dictionary
  • Why WebSphere 6.1 (JDK 1.5 Compliant) compiles my JSPs as JDK 1.4?
  • How would I make a polygon based on the relative positions of markers in google maps?
  • FormsAuthentication.CookieDomain property
  • Problem with output_buffering and php.ini
  • Java Arithmetic division
  • Multiple angular 2 apps and /api on same host with nginx
  • How do I hide and show the contents contents of a modal?
  • BREAK ON instruction in MySQL queries?
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Send HTML Mail with Unicode
  • jQuery colorbox breaks postbacks in ASP.NET Web Forms
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • How can I do case insensitive string search with Linq and SQL Server?
  • How to move to lines with the same indentation in Visual Studio Code
  • Firestore: Version history of documents
  • How to get “crispEdges” for SVG text?
  • Adding horizontal slider to QTableWidget
  • Allowing audio files in Spring MVC 3.0?
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Arraylist of strings into one comma separated string
  • Magento-Change Attribute of All Products
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • Ember.js + JQuery-UI Tooltip - Tooltip does not reflect the model / controller changes
  • Multiplying polynomials/simplifying like terms
  • Floating parent div grows to hypothetical width of floating child div
  • Comparing variables with strings bash
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • Jersey serializes character value to ASCII equivalent numeric string
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?