加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class NetType {
//网络判断
public boolean isCoon(Context context){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if(activeNetworkInfo!=null&&activeNetworkInfo.isAvailable()){
return true;
}
return false;
}
//没有网络是跳转至设置页面
public void openNetDialog(Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("打开网络页面");
builder.setMessage("当前没有网络,是否打开");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("取消",null);
AlertDialog dialog = builder.create();
dialog.show();
}
}