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 {

    public static boolean isNetConnected(Context context) {
        if (isWIFIConnected(context)) {
            return true;
        } else if (isGPRSConnected(context)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isWIFIConnected(Context context) {
        ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean result = true;
        NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (active == null || !active.isConnected()) {
            result = false;
        }
        return result;
    }

    public static boolean isGPRSConnected(Context context) {
        ConnectivityManager cmg = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean result = true;
        NetworkInfo active = cmg.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (active == null || !active.isConnected()) {
            result = false;
        }
        return result;
    }
}