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 {
    private static final int NT_WCDMA_PREFERRED = 0;
    private static final int NT_GSM_ONLY = 1;
    private static final int NT_WCDMA_ONLY = 2;
    private static final int NT_GSM_WCDMA_AUTO = 3;
    private static final int NT_CDMA_EVDO = 4;
    private static final int NT_CDMA_ONLY = 5;
    private static final int NT_EVDO_ONLY = 6;
    private static final int NT_GLOBAL = 7;
    private static final int NT_LTE_CDMA_EVDO = 8;
    private static final int NT_LTE_GSM_WCDMA = 9;
    private static final int NT_LTE_CMDA_EVDO_GSM_WCDMA = 10;
    private static final int NT_LTE_ONLY = 11;
    private static final int NT_LTE_WCDMA = 12;

    public static int hasActiveNetworkInfo(Context context) {
        int state = 0; // Assume disabled
        final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mActiveNetworkInfo = cm.getActiveNetworkInfo();
        if (mActiveNetworkInfo != null) {
            String typeName = mActiveNetworkInfo.getTypeName().toLowerCase();
            boolean isConnected = mActiveNetworkInfo.isConnectedOrConnecting();
            int type = mActiveNetworkInfo.getType();
            if ((isNetworkTypeMobile(type)) && (typeName.contains("mobile")) && isConnected)
                state = 2;
            else if ((!isNetworkTypeMobile(type)) && (!typeName.contains("mobile")) && isConnected)
                state = 1;
        }
        return state;
    }

    private static boolean isNetworkTypeMobile(int networkType) {
        switch (networkType) {
        case NT_WCDMA_PREFERRED:
        case NT_GSM_ONLY:
        case NT_WCDMA_ONLY:
        case NT_GSM_WCDMA_AUTO:
        case NT_CDMA_EVDO:
        case NT_CDMA_ONLY:
        case NT_EVDO_ONLY:
        case NT_GLOBAL:
        case NT_LTE_CDMA_EVDO:
        case NT_LTE_GSM_WCDMA:
        case NT_LTE_CMDA_EVDO_GSM_WCDMA:
        case NT_LTE_ONLY:
        case NT_LTE_WCDMA:
        case 14:
        case 15:
            return true;
        default:
            return false;
        }
    }
}