Q: how to run TimerTask off main UI thread? I am having trouble with a TimerTask Interfering with In App Purchasing (Async Tasks).
I am weak with Threads, so I believe it is running on the main UI thread, eating up resources.
How can I run this outside the UI thread? I have searched, and tried some suggestions using handlers. but seems like I get the same result, app gets really laggy.
when I don't run this task (refreshes every 500mS), the activity runs smoothly, and there are no hangs during In app purchases.
Your help is appreciated, code snippet below:
public class DummyButtonClickerActivity extends Activity {
        protected Timer timeTicker = new Timer("Ticker");
        private Handler timerHandler = new Handler();
        protected int timeTickDown = 20;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainhd);

    // start money earned timer handler
    TimerTask tick = new TimerTask() {
        public void run() {
            myTickTask();
        }
    }; 

    timeTicker.scheduleAtFixedRate(tick, 0, 500); // 500 ms each


} // End OnCreate



protected void myTickTask() {

    if (timeTickDown == 0) {

        /// run my code here
        //total = total + _Rate;





        timerHandler.post(doUpdateTimeout);
}
      else if(timeTickDown < 0) {
        // do nothing
    }

    timeTickDown--;

}

private Runnable doUpdateTimeout = new Runnable() {
    public void run() {

        updateTimeout();
    }
};



private void updateTimeout() {

    // reset tick
    timeTickDown = 2; // 2* 500ms == once a second



}

}

A: You can use HandlerThread that will run your Handler on a separate Thread
documentation:
 Handy class for starting a new thread that has a looper.
 The looper can then be used to create handler classes. Note that start() must still be called.

example:
     HandlerThread mHandlerThread = new HandlerThread("my-handler");
     mHandlerThread.start();
     Handler mHandler = new Handler(mHandlerThread.getLooper());

update:
 private Runnable doUpdateTimeout;
private HandlerThread mHandlerThread;
private Handler timerHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainhd);

    // start money earned timer handler
    TimerTask tick = new TimerTask() {
        public void run() {
            myTickTask();
        }
    }; 

    mHandlerThread = new HandlerThread("my-handler");
    mHandlerThread.start();

    timerHandler = new Handler(mHandlerThread.getLooper());
    doUpdateTimeout = new Runnable() {
        public void run() {

            updateTimeout();
        }
    };

    timeTicker.scheduleAtFixedRate(tick, 0, 500); // 500 ms each


} // End OnCreate



protected void myTickTask() {

    if (timeTickDown == 0) {

        /// run my code here
        //total = total + _Rate;

        timerHandler.post(doUpdateTimeout);
}
      else if(timeTickDown < 0) {
        // do nothing
    }

    timeTickDown--;

}





private void updateTimeout() {

    // reset tick
    timeTickDown = 2; // 2* 500ms == once a second

}
}

when you want to update the TextView from different thread
call this:
YOU_ACITIVITY_CLASS.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            //update here

        }
    });


A: The handler is running and processing in the main ui thread,so the  method doUpdateTimeout will be executed in the main thread.
In your code, after running 10 seconds later,the timeTickDown equals to 0 and code timerHandler.post(doUpdateTimeout); will be invoked,which will be executed in the main thread. Because it just let timeTickDown = 2; one second later,this code will be executed again(in the main ui thread) and then go on in each second.If there is some other code in doUpdateTimeout or updateTimeout,your main thread will be laggy.
Just change timerHandler.post(doUpdateTimeout); to updateTimeout()(call it directly and then execute it in the Timer thread,not the main ui thread).
