boolean available = NetWorkUtils.isNetWorkAvailable(this);
if (available) {
Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是wifi
boolean wifi = NetWorkUtils.isWifi(this);
if (wifi) {
Toast.makeText(MainActivity.this, "wifi网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "wifi网络连接失败", Toast.LENGTH_SHORT).show();
}
//使用自己编写的工具类,判断是否是手机流量
boolean mobile = NetWorkUtils.isMobile(this);
//有网做对应的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show();
//无网络时,跳转网络设置界面
Intent intent = new Intent("
com.bawei.net
");
intent.putExtra("net", "亲,断网了,应该去设置网络了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}
}
//判断网络是否连接
public static boolean isNetWorkAvailable(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
return false;
}
//判断是否是wifi
public static boolean isWifi(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
//判断是否是手机流量
public static boolean isMobile(Context context) {
//网络连接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}