首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android判断用户网络部类

2012-09-25 
android判断用户网络类型public class NetTestActivity extends Activity {private static final int CMNE

android判断用户网络类型

public class NetTestActivity extends Activity {private static final int CMNET = 3;private static final int CMWAP = 2;private static final int WIFI = 1;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getAPNType(this);}/** * 获取当前的网络状态 -1:没有网络 1:WIFI网络2:wap网络3:net网络 *  * @param context * @return */public static int getAPNType(Context context) {int netType = -1;ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();if (networkInfo == null) {return netType;}int nType = networkInfo.getType();System.out.println("networkInfo.getExtraInfo() is "+ networkInfo.getExtraInfo());if (nType == ConnectivityManager.TYPE_MOBILE) {if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {netType = CMNET;} else {netType = CMWAP;}} else if (nType == ConnectivityManager.TYPE_WIFI) {netType = WIFI;}return netType;}}

?

热点排行