Detect Data roaming in Android Application

You may want to know if the internet connection (data) is in roaming or not for the Android Application that you are working on. This can be done using ConnectivityManager class.


package com.code2care.networkdetection;
import android.content.Context;
public class CheckNetworkRoaming {
    // checkNetworkType detects what Network Connection type is active
    public String checkRoaming(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnected()) {
            return "no Connection";
        } else {
            NetworkInfo networkType = connManager.getActiveNetworkInfo();
            return "" + networkType.isRoaming();

package com.example.networkdetection;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class NetworkMainActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        CheckNetworkRoaming ck = new CheckNetworkRoaming();
                "Data Roaming : " +ck.checkRoaming(this) ,
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

Note: Remember to add user permission: android.permission.ACCESS_NETWORK_STATE to Android Manifest.

In order to test this functionality, you can turn data roaming on/off for your Android Emulator using DDMS tools.

Go to DDMS Perspective and goto Emulator Control tab and under Data dropdown select roaming to turn it on.

Try Out Code2care Dev Tools:


Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR -