is Network Connected - Android android.net

Android examples for android.net:ConnectivityManager

Description

is Network Connected

Demo Code

import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo == null) {
                return false;
            }//from  w w  w.  jav a2s  .c o  m
            return (mNetworkInfo.getState() == NetworkInfo.State.CONNECTED);
        }
        return false;
    }

}

Related Tutorials