Android: Check Mobile/Device network state

boolean isWiFi = false;
boolean isMobile = false;
boolean isBluetooth = false;

ConnectivityManager cm =
(ConnectivityManager) CommonUtil.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (null != activeNetwork) {
    boolean isConnected = activeNetwork.isConnectedOrConnecting();

    if (isConnected) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            isMobile = activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_BLUETOOTH) {
            isBluetooth = activeNetwork.getType() == ConnectivityManager.TYPE_BLUETOOTH;
        }
        return (isWiFi || isMobile || isBluetooth);
    }
}
return false;

Comments

Popular posts from this blog

Working with Android Hierarchy Viewer

Android: Standalone login with SQLiteOpenHelper