Detecting when roaming is turned off in Android
Asked Answered
W

2

9

I created a BroadcastReceiver that listens for a CONNECTIVITY_CHANGE event. For now, I simply print out the action of the Intent that calls the BroadcastReceiver and its extras. I'm using a test phone in our office here in Manila and the SIM card is from Denmark. When onReceive() is invoked, this is what gets printed out in LogCat:

action: android.net.conn.CONNECTIVITY_CHANGE
key: extraInfo, value: data.tre.dk
key: htcCurrentActiveNetwork, value: null
key: networkInfo, value: null
key: reason, value: roamingOn
key: noConnectivity, value: null
key: inetCondition, value: null

I need to know what the value of reason is when the roaming goes off--could be roamingOff by guessing, but I can't know for sure, unless I go to Denmark. This is because I need to perform a task when the roaming service gets turned off, and I noticed from LogCat that the CONNECTIVITY_CHANGE event is fired for reasons other than toggling the roaming modes. Or is there a better way of detecting when roaming is turned off?

Code for the broadcast receiver:

public void onReceive(Context context, Intent intent) {
    Log.d(This.LOGTAG, "action: " + intent.getAction());
    Bundle extras = intent.getExtras();
    for (String key : extras.keySet()) {
        Log.d(This.LOGTAG, "key: " + key + ", value: " + extras.getString(key));
    }
}

Manifest entry:

<receiver android:name=".listener.RoamingListener">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
Weighty answered 26/9, 2012 at 6:27 Comment(0)
W
1

There's a difference between network roaming and data roaming.

Generally, network roaming is allowing your telco to go through third-party satellites while you're outside the country so you can stay connected to their services. Even if you already told your telco that you want to subscribe to network roaming, it will NOT be activated until you actually begin connecting via third-party telco satellites, and that only happens when you're actually out of the country.

Data roaming is simply allowing/disallowing your device to consume mobile data while you are in network roaming mode. It's more device-dependent and you can easily turn it on or off in the phone's Settings app.

In short, if you want to detect network roaming, use TelephonyManager.isNetworkRoaming(). If you want to check whether data roaming is turned on or off, use Settings.Secure.getInt(getContentResolver(), Settings.Secure.DATA_ROAMING).

IMPORTANT: I haven't personally tested the following scenarios and I'm not sure what value isNetworkRoaming() will return.

  1. You didn't tell your home telco to turn network roaming on, but when you arrived at some other country, the phone began detecting other telco networks.
  2. After scenario 1, your phone asks you to choose which of the detected telco networks to connect to. You choose one and the phone attempts to connect. I don't know what value isNetworkRoaming() will return here because you didn't subscribe to network roaming in the first place.
Weighty answered 4/10, 2012 at 5:28 Comment(0)
F
0

You can simply use this line for detecting roaming state:

Settings.Secure.getInt(getContentResolver(), Settings.Secure.DATA_ROAMING)

This does not need any special permissions or receivers.

Source

Footrest answered 26/9, 2012 at 6:43 Comment(4)
I just logged the result of that line together with the result of TelephonyManager.isNetworkRoaming(). I found out that Settings.Secure.DATA_ROAMING may return a 0 (meaning, data roaming is off) while isNetworkRoaming() is true. I'm guessing this is because the telco is still in network roaming mode but I'm not allowing my device to use mobile internet. They seem to be different switches, and I'm after the network roaming mode.Weighty
And what do you want to know? I suppose that TelephonyManager.isNetworkRoaming() shows that you are in roaming, and setting shows if you can use internet in roamingFootrest
I'm after the network roaming mode because I'm trying to measure the user's next monthly bill--and that includes not only the amount of mobile data sent/received via data roaming.Weighty
OK, use TelephonyManager.isNetworkRoaming() for detecting if user is in roaming.Footrest

© 2022 - 2024 — McMap. All rights reserved.