I've looked at the code here as well as the code here but I still can't seem to get my code to work right. With the 2nd link, I can get a "timer" that counts up on the page, but with the first, my UI locks up. What I'm trying to do is have a seperate thread that continually flips the text in a textswitcher every 3 seconds as long as the app is open. I need it to switch between two values, and have tried something like the following:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
while(true){
try {
mHandler.post(new Runnable(){
@Override
public void run() {
try {
mSwitcher.setText("ON");
Thread.sleep(1000);
mSwitcher.setText("OFF");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
}catch (Exception e) {
//tv1.setText(e.toString());
}
}
}
};
Where it will flip "on" or "off" every 2 seconds. I also need to be able to update the text switcher content from the main UI, but haven't gotten to the point i can try and test that. In addition to the above, I have also tried to use an Async Task:
new AsyncTask<Void, Double, Void>() {
@Override
protected Void doInBackground(Void... params) {
while (true) {
mSwitcher.setText("ON");
SystemClock.sleep(2000);
mSwitcher.setText("OFF");
SystemClock.sleep(2000);
}
}
}.execute();
but this did not work either.