Android Network State Check isDeviceOnline(Activity activity)

Here you can find the source of isDeviceOnline(Activity activity)

Description

Iterates all network infos and returns true if one of them is available for sending data.

Parameter

Parameter Description
activity a parameter

Return

true if a network connection exists. Otherwise, flase is returned.

Declaration

public static boolean isDeviceOnline(Activity activity) 

Method Source Code

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

public class Main {
    /**//from w  w  w .  j  a v  a2  s .c o  m
     * Iterates all network infos and returns true if one of them is available for
     * sending data.
     * @param activity
     * @return true if a network connection exists. Otherwise, flase is returned.
     * @author Jason Myerscough
     */
    public static boolean isDeviceOnline(Activity activity) {
        ConnectivityManager cm = (ConnectivityManager) activity
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();

        if (netInfo != null) {
            for (int i = 0; i < netInfo.length; i++) {
                if (netInfo[i].isConnected())
                    return true;
            }
        }
        return false;
    }
}

Related

  1. isOpenNetwork(Context context)
  2. loadImageFromNetwork(String photoUrl)
  3. IsInternetReachable()
  4. setMobileDataEnabled(Activity a, Context context, boolean enabled)
  5. isNetworkAvailable(Activity mainActivity)
  6. isConnected(final Activity act)
  7. isOnline(Activity activity)
  8. CheckInternet(Activity a)
  9. checkInternetConnection(Context context)