Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

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

public class Main {
    public static boolean isConnectionWifi(Context context) {
        return !isConnection3G(context);
    }

    public static boolean isConnection3G(Context context) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (conMan != null) {
            if (conMan.getNetworkInfo(0) != null && conMan.getNetworkInfo(1) != null) {
                if (conMan.getNetworkInfo(0).getState() != null && conMan.getNetworkInfo(1).getState() != null) {
                    // mobile
                    State mobile = conMan.getNetworkInfo(0).getState();
                    // wifi
                    State wifi = conMan.getNetworkInfo(1).getState();
                    if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                        return false;
                    } else if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }

    }
}