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.State;

public class Main {

    public static int getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        State mobileState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        State wifiState = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (wifiState == State.CONNECTED || wifiState == State.CONNECTING) {
            return 1;
        } else if (mobileState == State.CONNECTED || mobileState == State.CONNECTING) {
            return 2;
        } else {
            return 0;
        }
    }
}