1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to monitor changes in mobile network connections?

Discussion in 'Android Development' started by Fractalis, Aug 23, 2019.

  1. Fractalis

    Fractalis Lurker
    Thread Starter

    Hello to all,

    I've been, for several days in different sites, looking for a way to monitor when a network change occurs without success so far. This is show with a Toast message when the phone changes from 2G to 3G, 3G to LTE and vice versa.

    I found that is needed to use TelephonyManager, Connectivity Manager and Broadcast Receiver.

    So far I have the below code that shows the changes when phone is in WiFi, without connection or mobile data, but once is in status "Mobile data" is not reporting the changes between 2G, 3G, LTE anymore.

    Is there a way to detect in the moment that occurs the mobile network type changes?

    This is a Utility class code that I have so far working for android 6.

    Code (Text):
    1.  
    2. class NetworkUtil{
    3.  
    4. public static String getConnectivityStatusString(Context context) {
    5.    String status = null;
    6.    String mobile_status = null;
    7.  
    8.    ConnectivityManager cm = (ConnectivityManager) context
    9.            .getSystemService(Context.CONNECTIVITY_SERVICE);
    10.  
    11.    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    12.  
    13.  
    14.    TelephonyManager mTelephonyManager = (TelephonyManager)
    15.            context.getSystemService(Context.TELEPHONY_SERVICE);
    16.    int networkType = mTelephonyManager.getNetworkType();
    17.    switch (networkType) {
    18.        case TelephonyManager.NETWORK_TYPE_GPRS:
    19.        case TelephonyManager.NETWORK_TYPE_EDGE:
    20.            mobile_status = "2G"; break;
    21.        case TelephonyManager.NETWORK_TYPE_UMTS:
    22.        case TelephonyManager.NETWORK_TYPE_HSPA:
    23.            mobile_status = "3G"; break;
    24.        default:
    25.            mobile_status = "Unknown"; break;
    26.    }
    27.  
    28.  
    29.    if (activeNetwork != null) {
    30.        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
    31.            status = "Wifi enabled";
    32.            return status;
    33.        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "2G") {
    34.            status = "2G enabled";
    35.            return status;
    36.        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "3G") {
    37.            status = "3G enabled";
    38.            return status;
    39.        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE && mobile_status == "Unknown") {
    40.            status = "Mobile unknown enabled";
    41.            return status;
    42.        }
    43.    } else {
    44.        status = "No internet is available";
    45.        return status;
    46.    }
    47.  
    48.    return status;
    49. }
    50. }
    Thanks for any help.
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...