创建一个网络工具类
public class NetWorkUtil {
private static ConnectivityManager connectivityManager;
//判断是否有网络链接
public static boolean HavaNetWork(Context context){
//获取网络链接者管理对象
connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取网络信息
NetworkInfo Info = connectivityManager.getActiveNetworkInfo();
if (Info==null || !Info.isConnected()){
return false;
}
if (Info.isRoaming()){
return false;
}
return true;
}
//判断是否有WIFI的方法
public static boolean isWiFi(){
if (connectivityManager!=null){
//拿到所有网络连接数据组
NetworkInfo[] Info = connectivityManager.getAllNetworkInfo();
if (Info!=null){
for(int i=0;i<Info.length;i++){
if (Info[i].getTypeName().equals("WIFI")&& Info[i].isConnected()){
return true;
}
}
}
}
return false;
}
public static boolean isMobileConnected(Context context){
NetworkInfo networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (networkInfo!=null){
return true;
}
return false;
}
}
BaseActivity 让Activity继承就Ok
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
boolean network = NetWorkUtil.HavaNetWork(this);
boolean wiFi = NetWorkUtil.isWiFi();
boolean mobile = NetWorkUtil.isMobileConnected(this);
if (network){
// Toast.makeText(this,"当前有网络",Toast.LENGTH_SHORT).show();
if (wiFi){
// Toast.makeText(this,"当前wifi连接",Toast.LENGTH_SHORT).show();
onNetwork();
}else{
// Toast.makeText(this,"当前wifi没有连接",Toast.LENGTH_SHORT).show();
if (mobile){
// Toast.makeText(this,"当前有流量",Toast.LENGTH_SHORT).show();
onNetwork();
}
}
}else{
// Toast.makeText(this,"当前无网络",Toast.LENGTH_SHORT).show();
noNetwork();
}
}
/**
* 有网络
*/
protected abstract void onNetwork();
/**
* 无网络
*/
protected void noNetwork(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_foreground);
builder.setTitle("网络提示信息");
builder.setMessage("网络不可用,如果继续,请先设置网络!");
builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = null;
/**
* 判断手机系统的版本!如果API大于10 就是3.0+
* 因为3.0以上的版本的设置和3.0以下的设置不一样,调用的方法不同
*/
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
startActivity(intent);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create();
builder.show();
}
}
MainActivity
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onNetwork() {
Toast.makeText(this, "有网络", Toast.LENGTH_SHORT).show();
//写网络请求接口的操作
}
}