网络连接的判断及网络类型的判断
2017-10-18 20:19
190 查看
直接调用以下方法即可
2.
//这是一个方法 public void checkMobileNet() { // 1.得到系统服务ConnectivityManager ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // 2.得到网络信息类对象 NetworkInfo networkInfo = manager.getActiveNetworkInfo(); // 3.进行判断 if (networkInfo != null && networkInfo.isConnected()) { Toast.makeText(this, "已连接互联网!", 0).show(); // 检测手机的网络类型 int type = networkInfo.getType();// 得到网络类型 switch (type) { case ConnectivityManager.TYPE_WIFI:// wifi网络 Toast.makeText(this, "wifi!", 0).show(); break; case ConnectivityManager.TYPE_MOBILE:// 移动数据 Toast.makeText(this, "移动数据!", 0).show(); // 2g 3g 4g getDetailMobileNetType(networkInfo); break; } } else { Toast.makeText(this, "未连接互联网!", 0).show(); AlertDialog.Builder buider=new AlertDialog.Builder(this); buider.setTitle("网络连接设置"); buider.setMessage("未连接网络,是否要进行设置?"); buider.setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { //跳转到系统设置网络的界面 Intent intent=new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent); } }); buider.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) {} }); buider.create().show(); } } //得到详细的网络类型 wap 2G/3G/4G网络** public void getDetailMobileNetType(NetworkInfo netInfo) { String strNetworkType = ""; // 得到整数类型 int subtype = netInfo.getSubtype(); // 得到名子 String _strSubTypeName = netInfo.getSubtypeName(); switch (subtype) { case TelephonyManager.NETWORK_TYPE_GPRS:// 联通2G case TelephonyManager.NETWORK_TYPE_EDGE:// 移动2G case TelephonyManager.NETWORK_TYPE_CDMA:// 电信2G case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: // api<8 : replace by 11 strNetworkType = "2G"; break; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: // api<9 : replace by 14 case TelephonyManager.NETWORK_TYPE_EHRPD: // api<11 : replace by 12 case TelephonyManager.NETWORK_TYPE_HSPAP: // api<13 : replace by 15 strNetworkType = "3G"; break; case TelephonyManager.NETWORK_TYPE_LTE: // api<11 : replace by 13 strNetworkType = "4G"; break; default: // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式 if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) { strNetworkType = "3G"; } else { strNetworkType = _strSubTypeName; } break; } Log.d("zzz", "网络类型是:" + strNetworkType); }
2.
创建一个类将代码赋值进去 import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.provider.Settings; public class WangLuo { public static boolean isWL(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); return (info != null && info.isAvailable()); } public static void TanKuang(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("网络链接中断,是否设置链接网络"); builder.setNegativeButton("取消", null); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 两种跳转都可以跳转到到设置网络 // Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); // intent.setAction("android.settings.WIRELESS_SETTINGS"); context.startActivity(intent); } }); builder.create().show(); } } /////////////////////////////////////////////// 在主方法中将下列代码复制上即可 //调用方法判断是否是联网状态 boolean wl = WangLuo.isWL(this); //判断是否联网如果没有跳转到网络连接界面连接网络 if (!wl){ WangLuo.TanKuang(this); }
相关文章推荐
- Unity判断网络连接类型
- 判断是否连接网络,当前网络类型,Gps是否打开
- [Android实例] 判断网络是否连接,然后选择网络类型(比如wifi等)来连接网络
- iphone最牛判断当前手机网络连接类型wifi-2G-3G-4G-5G
- android当前网络连接类型判断
- IOS判断当前网络连接类型(可区分2G和3G)
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- js判断手机连接网络类型
- Android判断网络是否连接以及连接类型
- Android: 判断网络连接状态及连接类型
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G(转载)
- android 判断是否有网络连接,判断网络连接类型
- android判断网络连接状态、联网类型、运营商
- Android之判断手机连接的网络类型是WIFI还是2G/3G/4G
- 网络连接类型判断
- android 网络连接状态判断与数据类型
- C#通过系统API判断网络连接类型及状态
- 网络连接的判断及网络类型的判断
- 手机网络连接类型判断 2/3/4G网络 WiFi和无网络 工具类
- 网络连接类型判断