检查网络,没有连接提示是否跳转设置页面打开网络
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)
NetworkInfo info = manager.getActiveNetworkInfo()
if (info != null && info.isConnected()) {
Toast.makeText(MainActivity.this, "网络连接连接", 0).show()
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
builder.setTitle("开启网络服务")
builder.setMessage("网络没有连接,请到设置进行网络设置!")
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT > 10) {
// 3.0以上打开设置界面,也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
startActivity(new Intent(
android.provider.Settings.ACTION_SETTINGS))
} else {
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS))
}
dialog.cancel()
}
})
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel()
}
})
builder.show()
}