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;

import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class Main {
    public static final String NETWORKTYPE_INVALID = "UNKNOWN";
    public static final String NETWORKTYPE_WAP = "WAP";
    public static final String NETWORKTYPE_WIFI = "WIFI";

    private static String mobileNetworkType(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) {
            return "TM==null";
        }
        switch (telephonyManager.getNetworkType()) {
        case TelephonyManager.NETWORK_TYPE_1xRTT:// ~ 50-100 kbps
            return "2G";
        case TelephonyManager.NETWORK_TYPE_CDMA:// ~ 14-64 kbps
            return "2G";
        case TelephonyManager.NETWORK_TYPE_EDGE:// ~ 50-100 kbps
            return "2G";
        case TelephonyManager.NETWORK_TYPE_EVDO_0:// ~ 400-1000 kbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_EVDO_A:// ~ 600-1400 kbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_GPRS:// ~ 100 kbps
            return "2G";
        case TelephonyManager.NETWORK_TYPE_HSDPA:// ~ 2-14 Mbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_HSPA:// ~ 700-1700 kbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_UMTS:// ~ 400-7000 kbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_EHRPD:// ~ 1-2 Mbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_EVDO_B: // ~ 5 Mbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_HSPAP:// ~ 10-20 Mbps
            return "3G";
        case TelephonyManager.NETWORK_TYPE_IDEN:// ~25 kbps
            return "2G";
        case TelephonyManager.NETWORK_TYPE_LTE:// ~ 10+ Mbps
            return "4G";
        case TelephonyManager.NETWORK_TYPE_UNKNOWN:
            return "UNKNOWN";
        default:
            return "4G";
        }
    }

    @SuppressWarnings({ "deprecation" })
    public static String getNetWorkType(Context context) {
        String mNetWorkType = null;
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager == null) {
            return "ConnectivityManager not found";
        }
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase("WIFI")) {
                mNetWorkType = NETWORKTYPE_WIFI;
            } else if (type.equalsIgnoreCase("MOBILE")) {
                String proxyHost = android.net.Proxy.getDefaultHost();
                if (TextUtils.isEmpty(proxyHost)) {
                    mNetWorkType = mobileNetworkType(context);
                } else {
                    mNetWorkType = NETWORKTYPE_WAP;
                }
            }
        } else {
            mNetWorkType = NETWORKTYPE_INVALID;
        }
        return mNetWorkType;
    }
}