Question

So I've been taking a look at how to do loops in Java using while methods, but they don't work for me. My Time functions is fairly simple but it doesn't update it self, so the time displayed on start would remain as is.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Time function
        TextView time = (TextView) findViewById(R.id.time);
        time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));

How exactly can I get my function to loop repeatedly, so there is no delay in time displaying. I've been trying many options but nothing seems to be working for me, I'm still learning v,v.

thanks if you help me out :)

EDIT - WHAT I DID

// Time function

final boolean keepRunning = true;
Thread thread = new Thread(){
    @Override
    public void run(){

        while(keepRunning){

            TextView time = (TextView) findViewById(R.id.time);
            time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));

            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    TextView time = (TextView) findViewById(R.id.time);
                    time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));;
                }
            });
        }
    }
};

thread.start();
Was it helpful?

Solution

Use a thread, loop inside that thread. Use runOnUiThread to update your text value (can't be done in worker thread).

boolean keepRunning = true;
Thread thread = new Thread(){
    @Override
    public void run(){

        while(keepRunning){

            // make the thread wait half a second (if you're only showing time up to seconds, it doesn't need to be updating constantly)
            try{
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    TextView time = (TextView) findViewById(R.id.time);
                    time.setText(DateUtils.formatDateTime(getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_12HOUR));
                }
            });
        }
    }
};

thread.start();

set keepRunning to false before exiting your app to stop the thread.

OTHER TIPS

From the developers website:

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }

}.start();

You may create a Handler and use it to trigger this event:

http://developer.android.com/reference/android/os/Handler.html

It has methods like to initiate an event in the future:

sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long)

The Handler itself can not do a "repeated event", but you can just handle the Message in the Handler and resend a new future event via the same sendMessage method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top