开发者

Broadcast Receiver to Check Internet Connectivity Results Fluctuate

开发者 https://www.devze.com 2023-04-12 01:52 出处:网络
Hello I am using following broadcast receiver code to check internet connectivity public class ConnectionChangeReceiver extends BroadcastReceiver{

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 t开发者_运维问答o the internet. What is causing this problem any idea?

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消