广播接收器检查互联网连接结果波动
您好,我正在使用以下广播接收器代码来检查互联网连接,
public class ConnectionChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive( Context context, Intent intent )
{
Bundle bundle = intent.getExtras();
NetworkInfo activeNetInfo = (NetworkInfo) bundle.getParcelable("networkInfo");
//On Network Re-Availability Display The Screen
if (activeNetInfo != null && activeNetInfo.isConnectedOrConnecting())
{
//If URL is not loaded load default URL else Resume
if (mWebView.getUrl()==null){
mWebView.loadUrl( Main.this.getString( R.string.app_url));
}
} //Else Show error message
else
{
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(Main.this.getString(R.string.Internet_connectivity_failure_message_title));
alertDialog.setMessage(Main.this.getString(R.string.Internet_connectivity_failure_message));
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {}
});
alertDialog.show();
}
}
}
当启用 WIFI 或移动网络时,它工作正常。但是,当两者都启用时,它会按预期使用 WIFI,但方法 activeNetInfo.isConnectedOrConnecting()
结果在 True
和 False
之间波动,因此无法连接到互联网。 是什么导致了这个问题?
谢谢。
Hello I am using following broadcast receiver code to check internet connectivity
public class ConnectionChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive( Context context, Intent intent )
{
Bundle bundle = intent.getExtras();
NetworkInfo activeNetInfo = (NetworkInfo) bundle.getParcelable("networkInfo");
//On Network Re-Availability Display The Screen
if (activeNetInfo != null && activeNetInfo.isConnectedOrConnecting())
{
//If URL is not loaded load default URL else Resume
if (mWebView.getUrl()==null){
mWebView.loadUrl( Main.this.getString( R.string.app_url));
}
} //Else Show error message
else
{
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(Main.this.getString(R.string.Internet_connectivity_failure_message_title));
alertDialog.setMessage(Main.this.getString(R.string.Internet_connectivity_failure_message));
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {}
});
alertDialog.show();
}
}
}
It works fine when either WIFI or Mobile Network is enabled. However when both are enabled, it uses WIFI as expected, but the method activeNetInfo.isConnectedOrConnecting()
result fluctuates between True
and False
, hence not connecting to the internet.
What is causing this problem any idea?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论