判断手机的联网类型:
01.public static void checkNetworkType(Context ctx) {02. ConnectivityManager connectivity = 03. (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);04. if (connectivity == null) {05. Log.e(Constants.TAG, "获取网络类型失败");06. return;07. } else {08. NetworkInfo info = connectivity.getActiveNetworkInfo();09. if(info == null) {10. Log.e(Constants.TAG, "获取网络类型失败");11. return;12. }13. if (info.getType() == ConnectivityManager.TYPE_MOBILE) {14. TelephonyManager tm = (TelephonyManager)15. ctx.getSystemService(Context.TELEPHONY_SERVICE);16. int type = tm.getNetworkType();17. switch (type) {18. case TelephonyManager.NETWORK_TYPE_EDGE:19. Log.d(Constants.TAG, "网络类型为EDGE");20. break;21. case TelephonyManager.NETWORK_TYPE_GPRS:22. Log.d(Constants.TAG, "网络类型为GPRS");23. break;24. case TelephonyManager.NETWORK_TYPE_UMTS:25. Log.d(Constants.TAG, "网络类型为UMTS");26. break;27. case TelephonyManager.NETWORK_TYPE_UNKNOWN:28. Log.d(Constants.TAG, "网络类型未知");29. break;30. }31. } else if (info.getType() == ConnectivityManager.TYPE_WIFI){32. Log.d(Constants.TAG, "网络类型为WIFI");33. }34. }35.}
没有评论:
发表评论