java - The Handler is not working properly in a function in Android -


following code wrote increasing brightness of background color of views 128 255 , vice-versa. unfortunately, handler supposed make wait not functioning properly. kindly me code.

there 3x3 matrix having 9 views in it. i'm randomly changing opacity of 1 cell.

level : number of cells want change 1 one. here, level : 3

color[9] : 3x3 matrix containing 9 views.

public void pattern()  {      for(int i=0;i<level;i++) {         int rand= 0 + (int)(math.random() * 8);         computer+=rand;         log.d(" :" , ""+i);         log.d(" random :" , ""+rand);         log.d("pattern incoming " , ""+color[rand].getbackground().getalpha());         color[rand].getbackground().setalpha(128);          final int random=rand;         handler.postdelayed(new runnable() {             @override             public void run() {                 color[random].getbackground().setalpha(128);                 log.d("inside handler " , ""+color[random].getbackground().getalpha());                 color[random].getbackground().setalpha(255);             }         },2000);          color[rand].getbackground().setalpha(128);          log.d("outside handler " , ""+color[rand].getbackground().getalpha());      } } 

android monitor logcat

11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 0 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 1 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 1 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 3 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 2 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 7 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 7 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128 

as can see "inside handler" printing @ end of loop runs 3 times. expecting "inside handler" execute after "pattern incoming" , before "outside handler" in following manner :

11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 0 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 1 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128     11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 1 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 3 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/ i :: 2 11-06 04:21:27.267 30640-30640/com.example.aman d/ random :: 7 11-06 04:21:27.267 30640-30640/com.example.aman d/pattern incoming: 128 $$ - 11-06 04:21:27.267 30640-30640/com.example.aman d/inside handler: 128 11-06 04:21:27.267 30640-30640/com.example.aman d/outside handler: 128 

the result getting in logcat expected. let's go through wrote:

(assume each statement takes 1ms)

1. first iteration of loop @ time 1 ms 2. print "i :: 0" 3. print "random :: 1" 4. print "pattern incoming :: 128" 5. post task `handler` set color 2 seconds (time 2005 ms) 6. print "outside handler :: 128" 7. go on next iteration of loop without waiting previous task  complete   8. second iteration of loop @ time 8 ms: 9. print print "i :: 1" 10. print "random :: 3" 11. print "pattern incoming :: 128" 12. post task `handler` set color 2 seconds (time 2012ms) .... **loop terminates** .... (finally long time after loop has completed first scheduled task triggered) .... 2005. print "inside handler: 128" 

the loop doesn't wait handler complete task before going on next iteration.

your task refactor code can effect want. have kind of iteration using code inside handler, rather dumping whole lot of tasks onto synchronously.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -