3

I need to get notified when mobile data gets enabled/disabled. For that purpose I am using BroadcastReceiver and register to ConnectivityManager.CONNECTIVITY_ACTION event. However that event is triggered only when Wi-Fi is disabled. As soon as I enable Wi-Fi I stop getting any event when enabling/disabling mobile data.

Any ideas? I need to receive mobile data state change event regardless of wi-fi state.

mkd156
  • 435
  • 3
  • 5
  • 16
  • If your using wifi, you will not get notified for mobile-data changes because you have connectivity. Unless connectivity changes from Wifi to mobile data, ConnectivityManager.CONNECTIVITY_ACTION will not notify you for mobile-data changes. You can get the specific state of mobile-data when being notified from ConnectivityManager.CONNECTIVITY_ACTION. Is this what you want ? – ehanoc Jun 13 '13 at 08:55
  • What you had described above is quite correct. I am not notified about mobile data changes when I am connected to wi-fi. And this is my problem. I want to be notified of mobile data changes regardless if I am connected to wi-fi or not. I need this because in my application I have an indicator which says mobile data is enabled/disabled. So when mobile data state changes I need to update UI accordingly to set it enabled/disabled. – mkd156 Jun 13 '13 at 09:04
  • I've got a solution for this. See my answer at http://stackoverflow.com/a/20932647/769265 – David Wasser Jan 06 '14 at 10:57
  • I am going through with the same problem. If you @mkd156 found solution for this issue please share here. It would be appreciated. – Pratik Aug 21 '18 at 14:36

4 Answers4

0
  1. Listen for ConnectivityManager.CONNECTIVITY_ACTION

  2. In your Broadcast Receiver, check for intent received and get Network Info

    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        final NetworkInfo networkInfo = 
            intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
    
  3. Check for

    if ((networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) && !networkInfo.isConnected()
    

    Mobile Data is not available

  4. Other wise in case

    if (networkInfo.isConnected() && (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
    

    Mobile Data is available

To Update your UI you can read the current state of Mobile Data Using Connectivity Manager like:

ConnectivityManager cm = (ConnectivityManager)applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);

public boolean isNetworkAvailable() {
        boolean status = false;
        try {

            final NetworkInfo netInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if ((netInfo != null) && (netInfo.getState() == NetworkInfo.State.CONNECTED)) {
                status = true;
            }
        } catch (final Exception e) {
            TLog.e(LOG, "Error Getting Mobile Network State");
            return false;
        }
        return status;

    }
Nargis
  • 4,687
  • 1
  • 28
  • 45
  • Regardless, according to the question posted; if he's using wifi, you will only be notified when wifi state changes anyway ... – ehanoc Jun 13 '13 at 09:02
  • I have done similar to what you are suggesting. It works very well when Wi-Fi is disabled. But when Wi-Fi is enabled and I start enabling/disabling mobile data, nothing happens, onReceive is never called. – mkd156 Jun 13 '13 at 09:07
0

The problem is that when a user is connected to WiFi the device will stop using the mobile data. So that Enabling and Disabling it will have no effect.

ConnectivityManager.CONNECTIVITY_ACTION just notifies of established or lost connections. So while you are on WiFi there is no Mobile Network connection to detect these changes on regardless of if its enabled or disabled.

Because of this Im not sure it will be possible to detect changes in mobile data state while connected to WiFi

James Grant
  • 217
  • 1
  • 11
  • Seems you are right. Do you know of some other event other than ConnectivityManager.CONNECTIVITY_ACTION that may work in my case? – mkd156 Jun 13 '13 at 09:10
  • Does the UI need to update instantly when the State is changed. Well you would have to leave your app and go to the setting page to turn off the Mobile data State right? So you could add a check to get the mobile data state into the Activity onResume function -- there is a good example of this here http://stackoverflow.com/a/12864897/2470460 – James Grant Jun 13 '13 at 09:15
  • The user may also enable mobile data from notifications page by sliding down notification drawer. But when the notification drawer is closed onResume isn't being called. – mkd156 Jun 13 '13 at 09:29
  • You could try the OnWindowFocusChanged method mentioned here - http://stackoverflow.com/a/6965537/2470460 – James Grant Jun 13 '13 at 09:34
  • Good idea. But unfortunately I am using Fragment not Activity. Seems fragment doesn't have that callback. – mkd156 Jun 13 '13 at 10:03
0

Well, not sure if you can rely on the notification from ConnectivityManager.CONNECTIVITY_ACTION in this particular case.

But, if you want to read if mobile-data setting is enabled/disabled at any point.

You can use the following :

ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

Method method = conn.getClass().getDeclaredMethod("getMobileDataEnabled");

boolean isMobileDataEnabled = (Boolean)method.invoke(conn);
ehanoc
  • 2,187
  • 18
  • 23
  • Thanks. I am successfully able to use getMobileDataEnabled and setMobileDataEnabled. What is really need is to get dynamically notified when that state changes. – mkd156 Jun 13 '13 at 09:31
0

I tested two action receiver.

1.

ConnectivityManager.CONNECTIVITY_ACTION;

It cannot receive mobile data enabled or disabled if wifi connnected.

2.

TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED = "android.intent.action.ANY_DATA_STATE" 

(It is hidden)

It can receive mobile data disabled if wifi connected., and cannot receive mobile data enabled.

My test device is Samsung Galaxy S4 mini LTE Korean Model(SHV-E370K) not Global Model (GT-I9195)

==========================================

If wifi connected, system does not call dataEnabled (because does not need mobile data).

So cannot receive mobile enabled state (Actually, mobile data is NOT enbaled)

I decided to schedule timer (period = 10000ms) and check getMobileDataEnabled().

private Method connectivityManager_getMobileDataEnabled = null;
private Method getConnectivityManager_getMobileDataEnabled() throws NoSuchMethodException {

    if (connectivityManager_getMobileDataEnabled == null) {
        connectivityManager_getMobileDataEnabled = ConnectivityManager.class.getMethod(
                "getMobileDataEnabled",
                new Class[0]);
    }

    return connectivityManager_getMobileDataEnabled;
}

public boolean getMobileDataEnabled() 
        throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    Method getMobileDataEnabled = getConnectivityManager_getMobileDataEnabled();
    getMobileDataEnabled.setAccessible(true);
    return (Boolean) getMobileDataEnabled.invoke(mConnectivityManager);
}
ChangUZ
  • 5,380
  • 10
  • 46
  • 64