Android: Monitoring Internet Connectivity Changes
To monitor internet connectivity changes, Android provides the ConnectivityManager.NetworkCallback class. This allows for more granular listening compared to the traditional BroadcastReceiver. Here's how to implement it:
<code class="java">public class ConnectivityMonitor extends ConnectivityManager.NetworkCallback { @Override public void onAvailable(Network network) { // Internet connectivity is available Log.d("Connectivity", "Internet connected"); } @Override public void onLost(Network network) { // Internet connectivity is lost Log.d("Connectivity", "Internet disconnected"); } }</code>
To register this callback, you need to create a ConnectivityManager instance and call the registerNetworkCallback method:
<code class="java">ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().build(), new ConnectivityMonitor());</code>
You can also use pre-defined connectivity states from ConnectivityManager:
<code class="java">if (connectivityManager.getActiveNetworkInfo() != null) { // Connected to a network if (connectivityManager.getActiveNetworkInfo().isConnectedToWifi()) { // Connected via WiFi } else { // Connected via mobile data } } else { // Not connected to a network }</code>
Remember to unregister the callback when you're done:
<code class="java">connectivityManager.unregisterNetworkCallback(new ConnectivityMonitor());</code>
The above is the detailed content of How can I monitor Internet connectivity changes in my Android application?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.