Java tutorial
//package com.java2s; /* * Copyright (C) 2014 The AppCan Open Source Project. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class Main { /** Unknown network class*/ public static final int NETWORK_CLASS_UNKNOWN = 0; /** Class of broadly defined "2G" networks*/ public static final int NETWORK_CLASS_2_G = 1; /** Class of broadly defined "3G" networks*/ public static final int NETWORK_CLASS_3_G = 2; /** Class of broadly defined "4G" networks*/ public static final int NETWORK_CLASS_4_G = 3; /** Class of broadly defined "WiFi" networks*/ public static final int NETWORK_CLASS_WIFI = 4; public static boolean wifiEnable(Context context) { return NETWORK_CLASS_WIFI == getConnectedType(context); } public static int getConnectedType(Context context) { ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo nInfo = cManager.getActiveNetworkInfo(); if (nInfo != null && nInfo.isAvailable()) { int type = nInfo.getType(); int subType = nInfo.getSubtype(); switch (type) { case ConnectivityManager.TYPE_MOBILE: switch (subType) { case 1://TelephonyManager.NETWORK_TYPE_GPRS: case 2://TelephonyManager.NETWORK_TYPE_EDGE: case 4://TelephonyManager.NETWORK_TYPE_CDMA: case 7://TelephonyManager.NETWORK_TYPE_1xRTT: case 11://TelephonyManager.NETWORK_TYPE_IDEN: return NETWORK_CLASS_2_G; case 3://TelephonyManager.NETWORK_TYPE_UMTS: case 5://TelephonyManager.NETWORK_TYPE_EVDO_0: case 6://TelephonyManager.NETWORK_TYPE_EVDO_A: case 8://TelephonyManager.NETWORK_TYPE_HSDPA: case 9://TelephonyManager.NETWORK_TYPE_HSUPA: case 10://TelephonyManager.NETWORK_TYPE_HSPA: case 12://TelephonyManager.NETWORK_TYPE_EVDO_B: case 14://TelephonyManager.NETWORK_TYPE_EHRPD: case 15://TelephonyManager.NETWORK_TYPE_HSPAP: return NETWORK_CLASS_3_G; case 13://TelephonyManager.NETWORK_TYPE_LTE: return NETWORK_CLASS_4_G; default: return NETWORK_CLASS_UNKNOWN; } case ConnectivityManager.TYPE_WIFI: return NETWORK_CLASS_WIFI; } } return NETWORK_CLASS_UNKNOWN; } }