简体   繁体   中英

Exiting from a loop on a runnable and move to next screen

I am almost a beginner on android. I am sort of stuck at a point. In the following code I am running a while loop for 30 times and runnable post delay is 3 seconds, means loop will run for 90 seconds(which is not important yet). It does so, thats fine. But in the runnable, when I call rangeofRouter0() function,the value in the textView changes to "You have reached the turn" when it goes into "else if" condition of rangeofRouter0() function and it must remove the callbacks as defined and move to next activity, exitiing from the while loop whether it has completed 90 seconds or not but program doesn't reach in if(textView.getText.equals....) part. And if I put this condition directly into "else if", callbacks are (I think) not removed and next screen is called again and again. I want that when code goes into else if, it should simply move to next screen and previous activity(means this activity), should simply be forgotten

    int i = 1;

    while (i<30)

    {
        myRunnable =new Runnable() {
            public void run() {
                scanWifiList();
                rangeofRouter0();

            }

        };

        handler1.postDelayed(myRunnable, i * 3000);

        i++;

        if(textView.getText().toString.equals("You have reached the turn")){
            ///this text gets in textView in "else if" condition

            handler1.removeCallbacks(myRunnable);
            Intent intent = new Intent(getApplicationContext(), FrontScreen.class);
            // finish();
            startActivity(intent);
            break;
        }
    }//While loop ended


  private void scanWifiList() {
    mainWifi.startScan();
    wifiList = mainWifi.getScanResults();
    textView5.setText("" + firstRouterDistance());

}


    public void rangeofRouter0(){     
    if(firstRouterDistance() >= 30 &&
            firstRouterDistance() <= 70 && ((degree>0 && degree <60)||(degree>300 && degree <360)))
    {
        textView.setText("Move left");
        speakOut(textView.getText().toString());
    }


    else if(firstRouterDistance() >= 3 &&
            firstRouterDistance() <= 20 &&
            secondRouterDistance() >= 7 &&
            secondRouterDistance() <= 60 && degree>220 && degree <250)
    {
        textView.setText("You have reached the turn");
        counter++;
    }
    else{
        textView.setText("Not before dining hall");
    }

}

You first need to understand how postDelayed() works exactly.

When you assign a runnable to the Handler's postDelayed() , that means you are scheduling a task to get executed at a particular time interval from the current time. Now what you are doing is running a while loop 29 times and each time scheduling the task - total 29 tasks to run at 3,6,9... seconds respectively. At this point you only scheduled the task and it's not started running yet. And you while loop finishes after iterating for 29 times. Your rangeofRouter0() never gets called during the while loop iterations since it took only a few milliseconds and hence counter is never greater than 0.

Note - If you want the loop to run for 30 times you should change it to while (i<=30) .

So, you need to change your logic to achieve what you want. If you want to read more about Handler . Check this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM