必须先加下面这两个权限,网络访问权限和差来看网络状态权限<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
if (isConnection(this)) {
下面这个方法是自定义的网络请求数据方法,就不上传了
getDataByWeb();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("网络链接中断,是否设置链接网络");
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 两种跳转都可以跳转到到设置网络
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
// intent.setAction("android.settings.WIRELESS_SETTINGS");
startActivity(intent);
}
});
builder.create().show();
}
}
public boolean isConnection(Context context) {
ConnectivityManager manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return (info != null && info.isAvailable());
}