BroadcastReceiver reconnected during delay

0

Hi. I have a BroadcastReceiver and it responds well. I am using a custom layout in case there is no connection that after 10 seconds the google dinosaur does not appear.  Now it turns out that in addition to completing the 10 seconds. if the connection returns within those 10 seconds it should stay on the page without going out. And I do not get it. I come 1 week almost without sleep to get to the broadcastreceiver. Which I did not know before that I should use and I was trying with TimerTask and Threads. Now I got stuck here. If I put everything into a task it is executed many times and it is not the idea. In case my problem is that I do not achieve that during those 10 seconds if the internet returns instead of taking me to the custom layout, I keep on the web page. I hope someone can help me. Greetings;)

public void checkInternetAcivity () {

IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
broadcastReceiver = new BroadcastReceiver() {
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onReceive(final Context context, Intent intent) {
        final int [] type = {ConnectivityManager.TYPE_WIFI,ConnectivityManager.TYPE_MOBILE};
        conexion = NetworkChangeReceiver.checkInternet(context,type);

        if (conexion) {

            setContentView(R.layout.activity_web_view);
            web = (WebView)findViewById(R.id.myVisor);
            webView();

        }else {

          /*  if (conexion){
                setContentView(R.layout.activity_web_view);
                web = (WebView)findViewById(R.id.myVisor);
                webView();
                handler.removeCallbacks(null);
            }*/

            handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {

                    setContentView(R.layout.activity_sin_conexion);
                    dialog.dismiss();
                }
            }, 10000);
        }
    }
};

I also try a cycle, but the same thing keeps happening to me.

 for (int i = 1; i <= 10; i++) {
                tareaLarga(); //sleep de 1 seg
                Toast.makeText(context, "conteo: "+ i, Toast.LENGTH_SHORT).show();
                if(!conexion && i == 10) {
                    setContentView(R.layout.activity_sin_conexion);
                }else if(conexion) {
                    webView();
                }

            }
    
asked by Jonatan Paez 27.11.2018 в 05:41
source

1 answer

0

Before wanting to try it, at 10 sec, no matter how hard the connection resumed, it always took me to the custom layout and it did not leave me on the web page.

I ended up solving the problem like this: This allowed me to resume connection after 10 sec, not leave the page.

if (conexion){
                webView();
            }else {

                handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {

                        if (!conexion) {
                            setContentView(R.layout.activity_sin_conexion);

                        }

                    }
                }, 10000);
            }
    
answered by 27.11.2018 / 23:35
source