android-How to check whether android phone is in roaming status?

1. Purpose

In this post, I would demo how to check if the android phone is in roaming status programmatically.

2. Environment

  • Android studio 3.x

3. The solution

3.1 What is roaming ?

Roaming means that your phone receives a cell signal whenever you’re outside your cell phone carrier’s operating area. In that case, your phone is roaming. … A Roaming icon appears at the top of the screen, in the status area, whenever you’re outside your cellular provider’s signal area.

3.2 How to check roaming status programmatically?

Here is the code:

public static boolean isRoaming(Context context) {
    boolean isDataRoamingDisabled = false;
    try {
        if (Build.VERSION.SDK_INT < 17) {
            isDataRoamingDisabled =
                    (Settings.System.getInt(context.getContentResolver(), Settings.Secure.DATA_ROAMING, 0)
                            == 0);
        } else {
            isDataRoamingDisabled =
                    (Settings.Global.getInt(context.getContentResolver(), Settings.Global.DATA_ROAMING, 0)
                            == 0);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return isDataRoamingDisabled;

}

3.3 How does it work?

We use Settings.Secure.DATA_ROAMING when phone’s android system version is below 17, because of the following reason:

Settings.Secure.DATA_ROAMING
This constant was deprecated in API level 17. Use Settings.Global.DATA_ROAMING instead

This API is deprecated in API level 17. If you target above API level 17(>=17), you can use this:

Settings.Global.DATA_ROAMING

Added in API level 17,Whether or not data roaming is enabled. (0 = false, 1 = true)

6. Summary

In this post, I demonstrated how to check if the android phone is in roaming status programmatically .