boolean available = NetWorkUtils.isNetWorkAvailable(this);
if (available) {
Toast.makeText(MainActivity.this, "網(wǎng)絡(luò)連接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "網(wǎng)絡(luò)連接失敗", Toast.LENGTH_SHORT).show();
}
//使用自己編寫的工具類,判斷是否是wifi
boolean wifi = NetWorkUtils.isWifi(this);
if (wifi) {
Toast.makeText(MainActivity.this, "wifi網(wǎng)絡(luò)連接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "wifi網(wǎng)絡(luò)連接失敗", Toast.LENGTH_SHORT).show();
}
//使用自己編寫的工具類,判斷是否是手機(jī)流量
boolean mobile = NetWorkUtils.isMobile(this);
//有網(wǎng)做對應(yīng)的操作
if (mobile) {
Toast.makeText(MainActivity.this, "手機(jī)流量網(wǎng)絡(luò)連接成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "手機(jī)流量網(wǎng)絡(luò)連接失敗", Toast.LENGTH_SHORT).show();
//無網(wǎng)絡(luò)時,跳轉(zhuǎn)網(wǎng)絡(luò)設(shè)置界面
Intent intent = new Intent("com.bawei.net");
intent.putExtra("net", "親噩死,斷網(wǎng)了,應(yīng)該去設(shè)置網(wǎng)絡(luò)了");
sendBroadcast(intent);
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
startActivity(wifiSettingsIntent);
}
}
神年、已维、、已日、垛耳、、飘千、堂鲜、、护奈、缔莲、、霉旗、痴奏、蛀骇、、读拆、擅憔、、檐晕、暑诸、、辟灰、攻擊類
//判斷網(wǎng)絡(luò)是否連接
public static boolean isNetWorkAvailable(Context context) {
//網(wǎng)絡(luò)連接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//網(wǎng)絡(luò)信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null) {
return true;
}
return false;
}
//判斷是否是wifi
public static boolean isWifi(Context context) {
//網(wǎng)絡(luò)連接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//網(wǎng)絡(luò)信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
//判斷是否是手機(jī)流量
public static boolean isMobile(Context context) {
//網(wǎng)絡(luò)連接管理器
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//網(wǎng)絡(luò)信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null && info.getType() == connectivityManager.TYPE_MOBILE) {
return true;
}
return false;
}