【发布时间】:2015-03-29 17:33:18
【问题描述】:
我在我的 android 应用程序中从服务器获取数据,同时检查 Internet 连接时,应用程序在没有 Internet 连接时崩溃。我正在使用默认的 http 连接来连接服务器。
检查互联网连接的代码是:
public void onClick(View view) {
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
//if it is connected to internet than start Another Activity.
startActivity(new Intent(SearchActivity.this, SearchActivity.class));
} else if (netInfo == null) {
AlertDialog alertDialog = new AlertDialog.Builder(ListViewExample.this).create();
alertDialog.setTitle("Connection Problem");
alertDialog.setMessage("You are not connected to Internet");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
}
【问题讨论】:
标签: android connectivity internet-connection