java - My android app is doing too much work on the main thread but I can't help it -


i'm writing android whack-a-mole game. know, moles pop on ui , timer text view changed every second. used handlers of these works. told use handlers instead of java.util.timer in question.

however, didn't work way wanted to. mean kind of worked timer text view's text changed irrationally. when checked logcat said i'm doing work on main thread.

back then, when developing winforms programs, problem never occurs. thinking putting handlers on thread know ui thread can access ui stuff, right?

here countdown class:

package com.whackanandroid;  import android.os.handler; import android.widget.textview;  public class countdown {     private handler handler;     private int timeleft;     private textview textview;     private boolean paused;     private countdownlistener listener;      private runnable countdowntask = new runnable () {         @override         public void run() {             if (!paused) {                 timeleft--;                 displaytime ();                 handler.postdelayed (this, 100);                 if (timeleft == 0) {                     pausecountdown ();                     if (listener != null)                         listener.oncountdownfinished ();                 }             }         }     };      private void displaytime () {         textview.settext (integer.tostring (timeleft / 10));     }      public void pausecountdown () {         paused = true;     }      public void startcountdown () {         paused = false;         handler.post (countdowntask);     }      public void setcountdownlistener (countdownlistener listener) {         this.listener = listener;     }      public countdown (textview tv) {         textview = tv;         timeleft = 600;         handler = new handler ();     } } 

and here phone class (the "mole")

package com.whackanandroid;  import android.os.handler; import android.view.view; import android.widget.imageview;  import java.util.random;  public class phone {     private imageview image;     private handler handler;     private random random;     private state state;     private boolean paused;      private runnable appeartask = new runnable () {         @override         public void run() {             if (!paused) {                 if (random.nextint (3) < 2) {                     setstate (state.android);                     game.getinstance ().addscore (10);                 } else {                     setstate (state.apple);                     game.getinstance ().addscore (-5);                 }                 handler.postdelayed (disappertask, random.nextint (1000) + 700);             }         }     };      private runnable disappertask = new runnable () {         @override         public void run() {             if (!paused) {                 setstate (state.none);                 handler.postdelayed (appeartask, random.nextint (2000) + 1000);             }         }     };       private view.onclicklistener imageonclick = new view.onclicklistener () {         @override         public void onclick(view v) {             //todo add onclicklistener         }     };      public void setstate (state value) {         state = value;         switch (state) {             case none:                 image.setimageresource (r.drawable.phone);                 break;             case android:                 image.setimageresource (r.drawable.androidphone);                 break;             case apple:                 image.setimageresource (r.drawable.applephone);                 break;         }     }      public state getstate () {         return state;     }      public void stoptimers () {         paused = true;     }      public void starttimers () {         paused = false;         if (getstate () == state.none) {             handler.postdelayed (appeartask, random.nextint (2000) + 1000);         } else {             handler.postdelayed (disappertask, random.nextint (1000) + 700);         }     }      public phone (imageview view, random r) {         view.setonclicklistener (imageonclick);         image = view;         random = r;         handler = new handler ();         image.setimageresource (r.drawable.phone);     } } 

i think code looks horrible. hope don't mind. if need more code, please tell me @ once.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -