Sudipta Som
Sudipta Som

Reputation: 6567

Check network in Android

How do I check any kind of network status in Android?

Upvotes: 0

Views: 1625

Answers (3)

Murugesan
Murugesan

Reputation: 43

can check network availability or not

ConnectivityManager connectivityManager 
    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

Upvotes: 0

Sreedev
Sreedev

Reputation: 6653

For checking the network this function will help you.

`public boolean InternetConnection() 

{
        ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }`

if the network is there it will return true otherwise it will return false.

Upvotes: 1

Mentalikryst
Mentalikryst

Reputation: 760

if (Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
        ConnectivityManager.TYPE_MOBILE)
    || Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(
        ConnectivityManager.TYPE_WIFI) {

    // do something
}

Upvotes: 4

Related Questions