Java tutorial
//package com.java2s; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class Main { public final static String NETWORK_CONN_MOBILE = "connect_mobile"; public final static String NETWORK_CONN_WIFI = "connect_wifi"; public static String getNetworkConnType(Context context) { ConnectivityManager connectMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (null == connectMgr) { return null; } NetworkInfo info = connectMgr.getActiveNetworkInfo(); if (info == null || !info.isConnected()) { return null; } if (info.isRoaming()) { // here is the roaming option you can change it if you want to // disable internet while roaming, just return false return null; } NetworkInfo mobileNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (null == mobileNetworkInfo) { return null; } NetworkInfo.State mobile = mobileNetworkInfo.getState(); if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) { return NETWORK_CONN_MOBILE; } NetworkInfo wifiNetworkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (null == wifiNetworkInfo) { return null; } NetworkInfo.State wifi = wifiNetworkInfo.getState(); if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) { return NETWORK_CONN_WIFI; } return null; } }