不多说了,直接看代码, NB人会懂的!
- package com.example.vdisktest;
-
- import android.app.Activity;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.Uri;
- import android.os.Bundle;
- import android.telephony.TelephonyManager;
- import android.text.TextUtils;
- import android.util.Log;
-
- public class NetTypeActivity extends Activity {
-
-
- public static final String CTWAP = "ctwap";
- public static final String CTNET = "ctnet";
- public static final String CMWAP = "cmwap";
- public static final String CMNET = "cmnet";
- public static final String NET_3G = "3gnet";
- public static final String WAP_3G = "3gwap";
- public static final String UNIWAP = "uniwap";
- public static final String UNINET = "uninet";
-
- public static final int TYPE_CT_WAP = 5;
- public static final int TYPE_CT_NET = 6;
- public static final int TYPE_CT_WAP_2G = 7;
- public static final int TYPE_CT_NET_2G = 8;
-
- public static final int TYPE_CM_WAP = 9;
- public static final int TYPE_CM_NET = 10;
- public static final int TYPE_CM_WAP_2G = 11;
- public static final int TYPE_CM_NET_2G = 12;
-
- public static final int TYPE_CU_WAP = 13;
- public static final int TYPE_CU_NET = 14;
- public static final int TYPE_CU_WAP_2G = 15;
- public static final int TYPE_CU_NET_2G = 16;
-
- public static final int TYPE_OTHER = 17;
-
- public static Uri PREFERRED_APN_URI = Uri
- .parse("content://telephony/carriers/preferapn");
-
-
- public static final int TYPE_NET_WORK_DISABLED = 0;
-
-
- public static final int TYPE_WIFI = 4;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- long start = System.currentTimeMillis();
- int checkNetworkType = checkNetworkType(this);
- Log.i("NetType", "===========elpase:"
- + (System.currentTimeMillis() - start));
-
- switch (checkNetworkType) {
- case TYPE_WIFI:
- Log.i("NetType", "================wifi");
- break;
- case TYPE_NET_WORK_DISABLED:
- Log.i("NetType", "================no network");
- break;
- case TYPE_CT_WAP:
- Log.i("NetType", "================ctwap");
- break;
- case TYPE_CT_WAP_2G:
- Log.i("NetType", "================ctwap_2g");
- break;
- case TYPE_CT_NET:
- Log.i("NetType", "================ctnet");
- break;
- case TYPE_CT_NET_2G:
- Log.i("NetType", "================ctnet_2g");
- break;
- case TYPE_CM_WAP:
- Log.i("NetType", "================cmwap");
- break;
- case TYPE_CM_WAP_2G:
- Log.i("NetType", "================cmwap_2g");
- break;
- case TYPE_CM_NET:
- Log.i("NetType", "================cmnet");
- break;
- case TYPE_CM_NET_2G:
- Log.i("NetType", "================cmnet_2g");
- break;
- case TYPE_CU_NET:
- Log.i("NetType", "================cunet");
- break;
- case TYPE_CU_NET_2G:
- Log.i("NetType", "================cunet_2g");
- break;
- case TYPE_CU_WAP:
- Log.i("NetType", "================cuwap");
- break;
- case TYPE_CU_WAP_2G:
- Log.i("NetType", "================cuwap_2g");
- break;
- case TYPE_OTHER:
- Log.i("NetType", "================other");
- break;
- default:
- break;
- }
- }
-
-
-
-
-
- public static int checkNetworkType(Context mContext) {
- try {
- final ConnectivityManager connectivityManager = (ConnectivityManager) mContext
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- final NetworkInfo mobNetInfoActivity = connectivityManager
- .getActiveNetworkInfo();
- if (mobNetInfoActivity == null || !mobNetInfoActivity.isAvailable()) {
-
-
-
-
-
- return TYPE_NET_WORK_DISABLED;
- } else {
-
- int netType = mobNetInfoActivity.getType();
- if (netType == ConnectivityManager.TYPE_WIFI) {
-
- return TYPE_WIFI;
- } else if (netType == ConnectivityManager.TYPE_MOBILE) {
-
-
-
-
-
-
-
- boolean is3G = isFastMobileNetwork(mContext);
-
- final Cursor c = mContext.getContentResolver().query(
- PREFERRED_APN_URI, null, null, null, null);
- if (c != null) {
- c.moveToFirst();
- final String user = c.getString(c
- .getColumnIndex("user"));
- if (!TextUtils.isEmpty(user)) {
- if (user.startsWith(CTWAP)) {
- return is3G ? TYPE_CT_WAP : TYPE_CT_WAP_2G;
- } else if (user.startsWith(CTNET)) {
- return is3G ? TYPE_CT_NET : TYPE_CT_NET_2G;
- }
- }
- }
- c.close();
-
-
-
-
-
-
-
-
- String netMode = mobNetInfoActivity.getExtraInfo();
- Log.i("", "==================netmode:" + netMode);
- if (netMode != null) {
-
- netMode = netMode.toLowerCase();
-
- if (netMode.equals(CMWAP)) {
- return is3G ? TYPE_CM_WAP : TYPE_CM_WAP_2G;
- } else if (netMode.equals(CMNET)) {
- return is3G ? TYPE_CM_NET : TYPE_CM_NET_2G;
- } else if (netMode.equals(NET_3G)
- || netMode.equals(UNINET)) {
- return is3G ? TYPE_CU_NET : TYPE_CU_NET_2G;
- } else if (netMode.equals(WAP_3G)
- || netMode.equals(UNIWAP)) {
- return is3G ? TYPE_CU_WAP : TYPE_CU_WAP_2G;
- }
- }
- }
- }
-
- } catch (Exception ex) {
- ex.printStackTrace();
- return TYPE_OTHER;
- }
-
- return TYPE_OTHER;
-
- }
-
- private static boolean isFastMobileNetwork(Context context) {
- TelephonyManager telephonyManager = (TelephonyManager) context
- .getSystemService(Context.TELEPHONY_SERVICE);
-
- switch (telephonyManager.getNetworkType()) {
- case TelephonyManager.NETWORK_TYPE_1xRTT:
- return false;
- case TelephonyManager.NETWORK_TYPE_CDMA:
- return false;
- case TelephonyManager.NETWORK_TYPE_EDGE:
- return false;
- case TelephonyManager.NETWORK_TYPE_EVDO_0:
- return true;
- case TelephonyManager.NETWORK_TYPE_EVDO_A:
- return true;
- case TelephonyManager.NETWORK_TYPE_GPRS:
- return false;
- case TelephonyManager.NETWORK_TYPE_HSDPA:
- return true;
- case TelephonyManager.NETWORK_TYPE_HSPA:
- return true;
- case TelephonyManager.NETWORK_TYPE_HSUPA:
- return true;
- case TelephonyManager.NETWORK_TYPE_UMTS:
- return true;
- case TelephonyManager.NETWORK_TYPE_EHRPD:
- return true;
- case TelephonyManager.NETWORK_TYPE_EVDO_B:
- return true;
- case TelephonyManager.NETWORK_TYPE_HSPAP:
- return true;
- case TelephonyManager.NETWORK_TYPE_IDEN:
- return false;
- case TelephonyManager.NETWORK_TYPE_LTE:
- return true;
- case TelephonyManager.NETWORK_TYPE_UNKNOWN:
- return false;
- default:
- return false;
-
- }
- }
-
- }
Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动,布布扣,bubuko.com
Android端如何获取手机当前的网络状态,比如wifi还是3G, 还是2G, 电信还是联通,还是移动
原文:http://www.cnblogs.com/Free-Thinker/p/3611315.html