title: How to detect the event of network type change Android?
body: <p>I have the following code that works to detect the current network type when is launched, but I would like the app detects when network type changes, for example from 3G to LTE, LTE to 3G, 3G to 2G, etc.</p>
  
  <p>I found the class <code>NetworkStateReceiver</code> for BroadcastReceiver <a href="https://stackoverflow.com/questions/25678216/android-internet-connectivity-change-listener">here</a>  but doesn't show anything when I change network.</p>
  
  <p>May someone help me how would be a way to not only shows the network type when app is launched but shows/detect the <code>event</code> of a network type change?</p>
  
  <p>I'm trying for Android 6.0.1</p>
  
  <pre><code>package com.example.myapp;
  
  import androidx.appcompat.app.AppCompatActivity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.os.Bundle;
  import android.telephony.TelephonyManager;
  import android.util.Log;
  
  
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          TelephonyManager teleMan = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
          int networkType = teleMan.getNetworkType();
  
          switch (networkType)
          {
              case 1:     Log.e("TAG", "GPRS");       break;
              case 2:     Log.e("TAG","EDGE");        break;
              case 3:     Log.e("TAG","UMTS");        break;
              case 13:    Log.e("TAG","LTE");             break;
          }
  
      }
  
      public class NetworkStateReceiver extends BroadcastReceiver
      {
          public void onReceive(Context context, Intent intent)
          {
              Log.e("app","Network connectivity change");
  
              if(intent.getExtras() != null)
              {
                  NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK);
                  if(ni != null &amp;&amp; ni.getState() == NetworkInfo.State.CONNECTED)
                  {
                      Log.e("app", "Network " + ni.getTypeName() + " connected");
                  }
              }
  
              if(intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE))
              {
                  Log.e("app", "There's no network connectivity");
              }
          }
      }
  }
  </code></pre>
  
  <p>The manifest I have is like this:</p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
          &lt;activity android:name=".MainActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                  &lt;action android:name="android.net.conn.CONNECTIVITY_CHANGE" /&gt;
                  &lt;action android:name="android.net.wifi.WIFI_STATE_CHANGED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  Thanks for any help.
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I've been looking without success for a way to trigger the event when a phone changes network type (i.e. from 2G to 3G, 3G to LTE and vice).</p>
  
  <p>I found some codes like this <a href="https://www.tutorialspoint.com/how-to-check-internet-connection-in-android#" rel="nofollow noreferrer">here</a> that works very well to trigger the event when the phone changes from "No internet connection" to "Wifi" or to "Mobile Data" using a <code>Broacaster Receiver</code> and <code>ConnectivityManager</code> with <code>.TYPE_WIFI</code> and <code>TYPE_MOBILE</code> and sending a Toast message to display to alert about the change. </p>
  
  <p>What I understand is that <code>ConnectivityManager</code> helps to trigger the event when changes from <code>WIFI</code> to <code>MOBILE</code> or viceversa, but when the phone is in status <code>TYPE_MOBILE</code> and there are changes in network type between 2G, 3G or LTE, there is no trigger generated for these changes. I even add <code>TelephonyManager</code> to handle the different <code>NETWORK_TYPEs</code> but it doesn't work.</p>
  
  <p>What is needed to do in order to trigger the event when the phone changes between 2G, 3G or LTE in the moment that occurs the change? </p>
  
  <p>I've incorporated the current answer but doesn't work for my goal.</p>
  
  <p>I'm trying for Android 6.</p>
  
  <p>The Util class I have so far is this:</p>
  
  <pre><code>class NetworkUtil{
  
  public static String getConnectivityStatusString(Context context) {
      String status = null;
      String mobile_status = null;
  
      ConnectivityManager cm = (ConnectivityManager) context
              .getSystemService(Context.CONNECTIVITY_SERVICE);
  
      NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  
  
      TelephonyManager mTelephonyManager = (TelephonyManager)
              context.getSystemService(Context.TELEPHONY_SERVICE);
      int networkType = mTelephonyManager.getNetworkType();
      switch (networkType) {
          case TelephonyManager.NETWORK_TYPE_GPRS:
          case TelephonyManager.NETWORK_TYPE_EDGE:
              mobile_status = "2G"; break;
          case TelephonyManager.NETWORK_TYPE_UMTS:
          case TelephonyManager.NETWORK_TYPE_HSPA:
              mobile_status = "3G"; break;
          default:
              mobile_status = "Unknown"; break;
      }
  
  
      if (activeNetwork != null) {
          if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
              status = "Wifi enabled";
              return status;
          } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE &amp;&amp; mobile_status == "2G") {
              status = "2G enabled";
              return status;
          } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE &amp;&amp; mobile_status == "3G") {
              status = "3G enabled";
              return status;
          } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE &amp;&amp; mobile_status == "Unknown") {
              status = "Mobile unknown enabled";
              return status;
          }
      } else {
          status = "No internet is available";
          return status;
      }
  
      return status;
  }
  </code></pre>
  
  <p>Thanks for any help.</p>