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;

public class Main {
    /**
     * method to check for network availability. returns true for available and
     * false for unavailable
     */
    public static boolean isConnectionAvailable(Context context) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileNetwork = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiNetwork != null && wifiNetwork.isAvailable() == true
                && wifiNetwork.isConnectedOrConnecting() == true) {
            return true;
        } else if (mobileNetwork != null && mobileNetwork.isAvailable() == true
                && mobileNetwork.isConnectedOrConnecting() == true) {
            return true;
        } else
            return false;
    }
}