public class NetTypeUtils {
public boolean isConn(Context context){
//1.得到网络管理类
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//2.通过管理类得到网络信息类对象
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if(activeNetworkInfo!=null && activeNetworkInfo.isAvailable()){
return true;
}else{
return false;
}
}
/**
* 在没有网络的时候 ,打开网络设置页面
* @param context
*/
public void openNetDialog(final 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) {
// 跳转到系统的网络设置界面
Intent intent = null;
// 先判断当前系统版本
if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
}
context.startActivity(intent);
}
});
builder.setNegativeButton("取消",null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
public boolean isConn(Context context){
//1.得到网络管理类
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//2.通过管理类得到网络信息类对象
NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
if(activeNetworkInfo!=null && activeNetworkInfo.isAvailable()){
return true;
}else{
return false;
}
}
/**
* 在没有网络的时候 ,打开网络设置页面
* @param context
*/
public void openNetDialog(final 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) {
// 跳转到系统的网络设置界面
Intent intent = null;
// 先判断当前系统版本
if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
}
context.startActivity(intent);
}
});
builder.setNegativeButton("取消",null);
AlertDialog dialog = builder.create();
dialog.show();
}
}