Try the following:
@Override public void run() { TextView tv1 = (TextView) findViewById(R.id.tv); while(true){ showTime(tv1); try { Thread.sleep(1000); }catch (Exception e) { tv1.setText(e.toString()); } } }
U can also try this
There is another way that you can use to update the user interface at a specific time interval. The above two parameters are correct, but depending on the situation, you can use alternative methods of updating the user interface in a certain period of time.
First declare one global varialbe for the handler to update the user interface control from the stream, for example below
Handler mHandler = new Handler (); Now create one thread and use the while loop to periodically execute the task using the sleep method on the thread.
new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(10000); mHandler.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // Write your code here to update the UI. } }); } catch (Exception e) { // TODO: handle exception } } } }).start();
Goofy
source share