0

Listen intent for ConnectivityManager.CONNECTIVITY_ACTION, it seems only for connection changes (connected/disconnected) or changes from wifi to mobile, etc.

But in mobile connection, if changes between 2G, 3G, 4G, cannot get any intent broadcast.

I want the app disable on 2G connection, or enable in 3G/4G connection. the app will run in background. So need to listen for changes.

Is it possible to achieve?

enter image description here

I also would like to know how system listen for changes of network type and show in the notification like the above image.

rodent_la
  • 1,195
  • 4
  • 18
  • 38

2 Answers2

2

By using getNetworkType method in TelephonyManager we can identify its 2G or 3G.

public String getNetworkClass(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int networkType = mTelephonyManager.getNetworkType();
switch (networkType) {
    case TelephonyManager.NETWORK_TYPE_GPRS:
    case TelephonyManager.NETWORK_TYPE_EDGE:
    case TelephonyManager.NETWORK_TYPE_CDMA:
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    case TelephonyManager.NETWORK_TYPE_IDEN:
         return "2G";
    case TelephonyManager.NETWORK_TYPE_UMTS:
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    case TelephonyManager.NETWORK_TYPE_HSPA:
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
    case TelephonyManager.NETWORK_TYPE_EHRPD:
    case TelephonyManager.NETWORK_TYPE_HSPAP:
         return "3G";
    case TelephonyManager.NETWORK_TYPE_LTE:
         return "4G";
    default:
         return "Unknown";
   }
    }

then use BroadcastReceiver

private class NetworkSwitcher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        return;
    }
    NetworkInfo networkInfo =
        (NetworkInfo)intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    if (networkInfo.isConnected()) {

            Log.d(TAG, "Network type: " + networkInfo.getTypeName() +
                    " Network subtype: " + networkInfo.getSubtypeName());

     // check your condition here

    } 
    else {
        Log.e(TAG, "Network connection lost");
    }
}
}

for more detail refer android.telephony.TelephonyManager

sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • Yes, already used that. But i need the notify way when changes occur dynamically without needing to do polling by myself. How the android top right notification achieve that? It will show changing to H or 3G or Lte ... – rodent_la Dec 12 '16 at 04:44
  • yes, already listen for ConnectivityManager.CONNECTIVITY_ACTION and at that time get the extra network info and check the subtype. But i am now testing with Android 6.0.1 on Nexus 5. It is not get called when mobile subtype changes... or the subtype is unknown... – rodent_la Dec 12 '16 at 04:56
  • did you set runtime permissions – sasikumar Dec 12 '16 at 05:06
  • 1
    yes, all the permission are good. Now i have found some solution. When CONNECTIVITY_ACTION received, if the subtype is unknown, I will get the telephony manager and call getNetworkType, that returns the correct subtype. But if cannot get intent. nothing can do. – rodent_la Dec 12 '16 at 05:12
0

Also need to listen to phonestatelistener for LISTEN_DATA_CONNECTION_STATE, this will report the mobile network type for each changes occur.

rodent_la
  • 1,195
  • 4
  • 18
  • 38