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>
有帮助吗?

解决方案

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.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top