Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Main {
    /**
     * Returns true if the app is connected to the internet, meaning the network
     * is connected.
     * 
     * @return True if the context can connect to the internet.
     */
    public static boolean isConnectedToInternet(Context context) {

        // check if we are connected to the network
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }

        // not connected to the network
        return false;
    }
}