package com.example.netconnected.util; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.v7.app.AlertDialog; /* * * 判断网络是否可用 * */ public class IsNetConnect { public static boolean isNetWorkConnected(Context context) { if (context != null) { // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); // 获取NetworkInfo对象 NetworkInfo networkInfo = manager.getActiveNetworkInfo(); //判断NetworkInfo对象是否为空 if (networkInfo != null) return networkInfo.isAvailable(); } return false; } /** * 网络状态不可用的时候弹出dialog,,,是否跳转设置 */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("当前网络不可用,是否设置") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到系统的网络设置界面 Intent intent = null; // 先判断当前系统版本 if (android.os.Build.VERSION.SDK_INT > 10) { // 3.0以上 intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); } else { intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); } context.startActivity(intent); } }).setNegativeButton("取消", null).show(); } }
判断网络是否可用
最新推荐文章于 2021-03-10 02:35:25 发布