get Network State - Android Network

Android examples for Network:Network Operation

Description

get Network State

Demo Code


//package com.java2s;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;

public class Main {

    public final static int NONE = 0;

    public final static int WIFI = 1;

    public final static int MOBILE = 2;

    public static int getNetworkState(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        // mobile
        State state = connManager.getNetworkInfo(
                ConnectivityManager.TYPE_MOBILE).getState();
        if (state == State.CONNECTED || state == State.CONNECTING) {
            return MOBILE;
        }//w w  w  .  ja  v a2  s . co m
        // Wifi
        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                .getState();
        if (state == State.CONNECTED || state == State.CONNECTING) {
            return WIFI;
        }
        return NONE;
    }
}

Related Tutorials