SoFunction
Updated on 2025-04-08

Android programming method to monitor network connection status changes

This article describes the method of Android programming to monitor network connection status changes. Share it for your reference, as follows:

BroadcastReceiver

public class MyReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    //(context, (), 1).show();
    ConnectivityManager manager = (ConnectivityManager) (Context.CONNECTIVITY_SERVICE);
    NetworkInfo mobileInfo = (ConnectivityManager.TYPE_MOBILE);
    NetworkInfo wifiInfo = (ConnectivityManager.TYPE_WIFI);
    NetworkInfo activeInfo = ();
    (context, "mobile:"+()+"\n"+"wifi:"+()
            +"\n"+"active:"+(), 1).show();
  } //If there is no network connection activeInfo is null}

Register BroadcastReceiver in manifest file

<receiver android:name=".MyReceiver">
  <intent-filter>
    <action android:name=".CONNECTIVITY_CHANGE"/>
  </intent-filter>
</receiver>

To read the network status, remember to add permissions

Copy the codeThe code is as follows:
<uses-permission android:name=".ACCESS_NETWORK_STATE"/>

For more information about Android related content, please check out the topic of this site:Android communication methods summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.