0

My Glass app is very simple. I have one static card and I set its text and display it in the overridden "onCreate()" method of my Activity:

myCard.setText("First text");
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);

I want to sleep for 5 seconds then display "Second Text" to the user. An earlier question on StackExchange discussed that you get a new view as above, and call setContentView() again.

This is fine so far, but my naive question is, where do I sleep and reset the content view? Obviously I can't sleep in "onCreate()" or "onStart()" of the activity, because the display has not been rendered for the user yet. I have a simple Service. In the service? Do I create a thread somewhere and use that? Thanks!

MikeN
  • 3
  • 2

1 Answers1

1

No need to start a new thread or sleep. You can do this with Android's Handler.postDelayed method, which posts a task to be executed on the UI thread at a later time.

public class MyActivity {
    private Handler mHandler = new Handler();

    @Override
    protected boolean onCreate() {
        myCard.setText("First text");
        View cardView = myCard.getView();
        // Display the card we just created
        setContentView(cardView);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateCard();
            }
        }, 5000 /* 5 sec in millis */);
    }

    private void updateCard() {
        // update the card with "Second text"
    }
}
Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • I used this and it worked! Thanks a lot! Later on I will need a more complex updating method. I had first tried a Thread as follows. – MikeN Aug 06 '14 at 19:04
  • Second try from a StackOverflow newbie... I had first tried a Thread as follows. Declare Thread in MyActivity. Initialize it in onCreate(), override run() to do sleep and setContentView() as needed. But when run, the app displays the first text, and quits after 5 sec, without showing the new text set in the Thread. I could start a new question and post the code, but perhaps you can point out some obvious conceptual error... – MikeN Aug 06 '14 at 19:13