is Network Connected - Android Phone

Android examples for Phone:Network

Description

is Network Connected

Demo Code


//package com.java2s;

import android.content.Context;

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

public class Main {
    public static boolean isNetworkConnected(Context context) {
        return isWifiConnected(context) || isMobileConnected(context);
    }/*from  w  w w.  j ava  2s  .c o  m*/

    public static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        boolean isWifiConnected = ni.isConnected();
        return isWifiConnected;
    }

    public static boolean isMobileConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        boolean isMobileConnected = ni.isConnected();
        return ni.isConnected();
    }
}

Related Tutorials