Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Main {
    public static boolean isNetworkAvailable(Context context) {
        try {
            //define object of ConnectivityManager
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            //define object of NetworkInfo and get detailed info about
            // the currently active default data network from ConnectivityManager
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

            //check connection
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        } catch (Exception ex) {
            Log.e("Helper", "isNetworkAvailable: " + ex.getMessage());
            return false;
        }
    }
}