Android, creating a simple thread that will updated my seconds counter

Ryan picture Ryan · Mar 20, 2011 · Viewed 13.3k times · Source

Basically, I am trying to run a seconds counter and a levels counter. For every 10 seconds I want to ++level.
But that's not implemented as yet, so far I am just trying to get the seconds to display but I am getting runtime exceptions and a crash.
Googling I see that its because I am trying to update the UI from my thread and thats not allowed. So I guess I am going to need asyncTask, but I have no idea how to do that with my simple little program. Please help or give me some alternatives...

package com.ryan1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class main extends Activity {

int level = 1;
int seconds_running=0;

TextView the_seconds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    the_seconds = (TextView) findViewById(R.id.textview_seconds);



    Thread thread1 = new Thread(){
        public void run(){
            try {
                sleep(1000); Log.d("RYAN", " RYAN ");

                updated_secs();
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("RYAN", " "+e);
            }
        }
    };
    thread1.start();
}

public void updated_secs(){
    seconds_running++;
    the_seconds.setText(" "+seconds_running);
}
}

Answer

Ryan Reeves picture Ryan Reeves · Mar 20, 2011

Create a Handler in your UI thread, then in the worker thread send a message to the handler (Handler.sendMessage(...)).

The message will be processed on your UI thread, so you can update the text widget correctly. Like this:

private Handler myUIHandler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        if (msg.what == some_id_you_created)
        {
            //Update UI here...
        }
    }
};

Then in your thread, to send a message to the handler you do this:

Message theMessage = myHandler.obtainMessage(some_id_you_created);
myUIHandler.sendMessage(theMessage);//Sends the message to the UI handler.