0

I am beginner in android.

What I want: Here I am trying to achieve that sub handler should call 10 times of every second of main handler. And that main handler should continue until 20 seconds.

Issue: To check that i\I have used log but its not working. It goes into sub handler some times 8 times or some times 9 or 10. Is there any logical error or is there any other better way to achieve this? Thank you.

My code:

int i=0;
int cont;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        i++;
        cont = 1;
        Log.e("main count", i + "");
        final Handler handler1 = new Handler();
        handler1.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (cont <= 10) {
                    Log.e("sub count ", cont + "");
                    cont++;
                    handler1.postDelayed(this, 100);
                }
            }
        }, 100);
        if (!(i == 20)) {
            handler.postDelayed(this, 1000);
        }
    }
}, 1000);
lrnzcig
  • 3,868
  • 4
  • 36
  • 50
Chirag
  • 27
  • 7
  • @pskink-Actually in future i want to call different methods in both main handler and other sub handler.. – Chirag Sep 29 '17 at 07:31
  • Why do you need multiple Handlers? – JimmyB Sep 29 '17 at 09:55
  • @jimmyB-I want the flaw that every single second the y method gets called multiple times and from main handler x method gets called one time.. – Chirag Sep 29 '17 at 10:08
  • you're going to have to format your code and use descriptive variable names if you want us to understand what you are doing – Tim Sep 29 '17 at 14:45

1 Answers1

0

It's difficult to execute code with precise timing using a Handler because you have to measure the time yourself. It's much easier to use the built-in classes like ScheduledThreadPoolExecutor or Timer. I'd recommend the first as it would require less changes to the code. You should pick one of the scheduleAtFixedRate() or scheduleWithFixedDelay() methods according to your requirements.

final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(2);
final ScheduledFuture mainFuture = executor.scheduleWithFixedDelay(new Runnable() {
    public void run() {
       if (i < 20) {
           Log.e("main count ", i + "");
           i++;
       } else {
           mainFuture.cancel(true);// stop after 20 executions
       }          
},
0, /*initial wait time*/
1, /* time between consecutive runs */
TimeUnit.SECONDS);

final ScheduledFuture subFuture = executor.scheduleWithFixedDelay(new Runnable() {
    public void run() {
       if (cont < 20 * 10) {
           Log.e("sub count ", cont + "");
           cont++;
       } else {
           subFuture.cancel(true);// stop after 200 executions
           executor.shutdown();
       }          
}, 
100, /*adjust this according to the desired sequencing of the count tasks */
100, /* time between consecutive runs */
TimeUnit.MILLISECONDS);

You could also use a single ScheduledFuture and execute the code for main count after every 10 executions of the sub count code.

Crispert
  • 1,102
  • 7
  • 13