弹出对话框调用:
ConnectivityManager con = (ConnectivityManager)
getSystemService(MainActivity.CONNECTIVITY_SERVICE);
boolean wifi = con.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting();
boolean internet = con.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
if (wifi | internet) {//检测网络是否开启
方法();//如果开启了网络要走的事情
} else {
new AlertDialog.Builder(this)
.setMessage("网络未连接,现在是否开启?")
.setPositiveButton("开启",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
if (android.os.Build.VERSION.SDK_INT > 13) {
startActivity(new Intent(
android.provider.Settings.ACTION_SETTINGS));
} else {
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
arg0.cancel();
}
}).setNegativeButton("取消", null).show();
}