Home  >  Article  >  Java  >  How to Detect Network Status Changes and Offline Events on Android?

How to Detect Network Status Changes and Offline Events on Android?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 19:51:02659browse

How to Detect Network Status Changes and Offline Events on Android?

Monitoring Network Status Changes on Android

Question: How can I detect changes in the network status on an Android device, specifically when the network goes offline?

To capture network status changes, Android provides the BroadcastReceiver class. The ConnectivityManager class allows you to get information about the active network connection.

Solution:

1. Create a Java class extending BroadcastReceiver

<code class="java">public class ConnectionChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (activeNetInfo != null) {
            Toast.makeText(context, "Active Network Type: " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
        }

        if (mobNetInfo != null) {
            Toast.makeText(context, "Mobile Network Type: " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT).show();
        }
    }
}</code>

2. Add necessary permissions to the AndroidManifest.xml

<code class="xml"><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></code>

3. Register the BroadcastReceiver in the AndroidManifest.xml

<code class="xml"><receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
          android:label="NetworkConnection">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver></code>

4. Register the BroadcastReceiver dynamically (optional)

If you need to register the BroadcastReceiver dynamically in your code, you can use the following approach:

<code class="java">BroadcastReceiver receiver = new ConnectionChangeReceiver();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
context.registerReceiver(receiver, filter);</code>

This code will allow you to capture network status changes and determine when the network goes offline. You can handle the event accordingly in your application logic.

The above is the detailed content of How to Detect Network Status Changes and Offline Events on Android?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn