Determines the network connection state. - Android Network

Android examples for Network:Network Status

Description

Determines the network connection state.

Demo Code


//package com.java2s;
import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    /**// ww  w .ja v  a  2  s  . c om
     * Determines the network connection state. Network can be either Wifi/Data.
     * 
     * @param context A valid context
     * @return {@code true} if a trace of an active Wifi/data connection found,
     *         {@code false} otherwise
     */
    public static boolean isInternetConnectionAvailable(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        return netInfo != null && netInfo.isAvailable()
                && netInfo.isConnectedOrConnecting();

    }
}

Related Tutorials