我正在进行一个实时项目,当用户点击应用程序时,欢迎屏幕出现(该屏幕上有一个Webview)。如果没有连接到互联网,则应用程序会崩溃。我的问题是通过编程方式检查移动设备是否连接到互联网。如果没有连接,则不要从Web服务抓取数据到Webview中,并显示一个对话框,显示“检查您的互联网连接”。
在研究过程中,我发现了许多事情,并尝试着去实现它,但是没有满足我的需求。
我的代码是:
在研究过程中,我发现了许多事情,并尝试着去实现它,但是没有满足我的需求。
我的代码是:
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
else
{
Description.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(WelcomePage.this)
.setTitle(getResources().getString(R.string.app_name))
.setMessage(
getResources().getString(
R.string.internet_error))
.setPositiveButton("OK", null).show();
}
return false;
}
我正在AsyncTask
的doInBackground()
中调用此函数。
请帮忙!