首先建一个工具类来判断手机网络
掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
(切记注册权限)
<uses-permission
android
:name=
"android.permission.INTERNET"
/>
1.自定义广播类
2.完成注册
//注册广播 MyRecever recever = new MyRecever(); IntentFilter filter = new IntentFilter(); filter.addAction("com.bawei.net"); registerReceiver(recever, filter); |
private class MyRecever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals("com.bawei.net")){ String net = intent.getStringExtra("net"); Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show(); } } } |
3.编写网络判断的工具类
(1).判断手机网络是否连接
//判断网络是否连接 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; } |
(2).判断是否是wifi
//判断是否是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; } |
(3).判断是否是手机流量
//判断是否是手机流量 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; } |
4.使用网络判断的工具类,判断当前用户手机的网络情况
(1).
//使用自己编写的工具类,判断网络是否连接 boolean available = NetWorkUtils.isNetWorkAvailable(this); if (available) { Toast.makeText(MainActivity.this, "网络连接成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show(); } |
(2).
//使用自己编写的工具类,判断是否是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(); } |
(3).
//使用自己编写的工具类,判断是否是手机流量 boolean mobile = NetWorkUtils.isMobile(this); //有网做对应的操作 if (mobile) { Toast.makeText(MainActivity.this, "手机流量网络连接成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "手机流量网络连接失败", Toast.LENGTH_SHORT).show(); } } |
5.无网络时,跳转网络设置界面
//无网络时,跳转网络设置界面 Intent intent = new Intent("com.bawei.net"); intent.putExtra("net", "亲,断网了,应该去设置网络了"); sendBroadcast(intent); Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS"); startActivity(wifiSettingsIntent); |
下面是具体的代码
1.工具类:NetWorkUtils
package com.bawei.network.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 网络判断的工具类
*/
public class NetWorkUtils {
//判断网络是否连接
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;
}
}
2.在MainActivity中检测
package com.bawei.network.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.bawei.network.R;
import com.bawei.network.utils.NetWorkUtils;
/**
* 掌握网络是否连接以及网络类型的判断,掌握无网络情况下,跳转设置网络设置界面
* 1.自定义广播类
* 2.完成注册
* 3.编写网络判断的工具类
* 4.使用网络判断的工具类,判断当前用户手机的网络情况
* 5.无网络时,跳转网络设置界面
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播
MyRecever recever = new MyRecever();
IntentFilter filter = new IntentFilter();
filter.addAction("com.bawei.net");
registerReceiver(recever, filter);
//使用自己编写的工具类,判断网络是否连接
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);
}
}
private class MyRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.bawei.net")){
String net = intent.getStringExtra("net");
Toast.makeText(MainActivity.this, "接收广播成功:"+net, Toast.LENGTH_SHORT).show();
}
}
}
}
获取手机网络判断是否有网,没有网络弹出系统对话框
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//huo qu 手机连接管理器
ConnectivityManager manager =
(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
//获取网络信息
NetworkInfo info = manager.getActiveNetworkInfo();
//判断手机是否有网络
if (info == null) {
//对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//设置标题
builder.setTitle("提示");
//设置消息
builder.setMessage("没有网络,是否进入WIFI设置界面?");
//确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
});
//取消按钮
builder.setNegativeButton("取消", null);
//创建对话框
AlertDialog dialog = builder.create();
//显示对话框
dialog.show();
} else {
}
}
}
下面这是弹出系统的对话框
//对话框 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设置标题 builder.setTitle("提示"); //设置消息 builder.setMessage("没有网络,是否进入WIFI设置界面?"); //确定按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS)); } }); //取消按钮 builder.setNegativeButton("取消", null); //创建对话框 AlertDialog dialog = builder.create(); //显示对话框 dialog.show(); |