There is a cycle:

void someTask() { TimerTask task = new TimerTask() { public void run() { while (parseRep < 2) { Document doc = null; try { Connection.Response response = Jsoup.connect("http://mobayle.ru/aut.php") .method(Connection.Method.GET) .execute(); response = Jsoup.connect("http://mobayle.ru/login.php") .header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8") .data("nick", loginAuth ,"pass", passAuth) .method(Connection.Method.POST) .timeout(30000).execute(); doc = Jsoup.connect("http://mobayle.ru").cookies(response.cookies()).get(); Element blockRega = doc.select("div.foot").first(); nav2Reg = blockRega.text(); Element usInfo = doc.select("div.nav1").first(); usName = usInfo.text(); Elements kollMess = doc.select("a[href*=new_mess] div.nav2 font[color=red]"); kollNewMess = kollMess.text(); System.out.println(kollNewMess); } catch (IOException e) { e.printStackTrace(); } Log.d(LOG_TAG, nav2Reg); } //while } }; new Timer().schedule(task, 1000, 10000); } 

How can I make the repeated execution of the cycle begin only when 10 seconds have passed since the last execution? I would be grateful for the help.

    1 answer 1

    seeing the .start () method; I will assume that you use the class Thread. I suggest using TimerTask:

     Timer timer = new Timer(); TimerTask timerTask = new TimerTask() { public void run() { //ваш код } }; timer.schedule(timerTask, 5000, 10000);//первое число - когда он //запуститься, второе - через сколько будет повторяться 

    If only once it needs to be done, use the Handler:

     Runnable runnable = new Runnable() { //ваш код }; Handler h = new Handler(); h.postDelayed(runnable, 10000); 

    And remember - in which thread the Handler was defined with the default constructor, in which it will work. In this case, the run () method in which the loop is running will be the one.

    • Sorry. Could you add the timer code to my code (edited in the question) - for some reason I swear at the token - iKey