Using time in app

Put your problem here if it does not fit any of the other categories.

Using time in app

Postby ramgraph1 » Fri Feb 01, 2008 2:12 pm

I want to do the seemingly simple task of having something happen after a few seconds have passed in an app. For example, the app starts and after five seconds a TextView is changed using setText. But what is the simplest way of having things wait those five seconds? I have looked at the Pizza Timer tut and System.currentTimeMillis(). They both look rather complex for what I want. I've used some JavaScript and there you can just use "yield" and state the number of seconds. Is there a similar simple solution in Android? Thanks.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

I think you want the sleep() command

Postby don » Fri Feb 01, 2008 3:21 pm

Take a look here:
android doc

I haven't used this so I am not 100% sure, but it may help -- good luck!
don
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Dec 07, 2007 5:17 am

Postby plusminus » Fri Feb 01, 2008 4:43 pm

Hello ramgraph1,

you could simply send your GUI-Thread to sleep(5000); BUT that would block your whole GUI for that time.

The best way is to use a separated Thread and a Handler like I did it in the Pizza-Timer-Tutorial.
The following function (from android.os.Handler) probably fits your needs:
public final boolean postDelayed(Runnable r, long delayMillis)


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Sun Feb 03, 2008 2:57 pm

Thanks for replying. I've read through the Pizza Timer and about SystemClock more, as well as the Alarm Controller example. But, I have to admit I am clueless as to how to use
Code: Select all
public final boolean postDelayed(Runnable r, long delayMillis)

Does anyone know of any fairly simple examples of this code or something similar that would help me comprehend how to use it?
Or... it seems to me this code:
Code: Select all
long timecheck = SystemClock.elapsedRealtime();

might be made to work. The AlarmController uses this code:

Code: Select all
long firstTime = SystemClock.elapsedRealtime();
            firstTime += 15*1000;

            // Schedule the alarm!
            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                            firstTime, 15*1000, intent);

but the Android docs say you should use the alarm only if you might need to wake the phone up, otherwise use a handler. Couldn't this be adapted to be used with a handler? If so, is there any advice on how to do that?
Thanks in advance.
Last edited by ramgraph1 on Sun Feb 03, 2008 3:10 pm, edited 1 time in total.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Postby plusminus » Sun Feb 03, 2008 3:09 pm

Hello ramgraph,

you'll create a Handler first, then put a Runnable, which is "~ a Thread" who's run()-method will get executed by the Handler after X milliseconds.
You may ask, why not creating a Thread directly :?: :arrow: Very often (and very probably in this case) you need the Thread that created the View(/Activity) to do the work (and not another thread!), because of security reasons.
Exactly that is what the Handler class accomplishes.

A well working example:
I'm using the following code to in a SplashScreen. It will redirect to the next Activity within X milliseconds:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onCreate(Bundle icicle) {
  2.                 super.onCreate(icicle);
  3.                 setContentView(R.layout.splashscreen);
  4.                
  5.                 /* New Handler to start the Menu-Activity
  6.                  * and close this Splash-Screen after some seconds.*/
  7.                 new Handler().postDelayed(new Runnable(){
  8.                         @Override
  9.                         public void run() {
  10.                                 /* Create an Intent that will start the Menu-Activity. */
  11.                                 Intent mainIntent = new Intent(Splash.this,Menu.class);
  12.                                 Splash.this.startActivity(mainIntent);
  13.                                 Splash.this.finish();
  14.                         }
  15.                 }, SPLASH_DISPLAY_LENGHT);
  16.         }
Parsed in 0.052 seconds, using GeSHi 1.0.8.4


I hope I described it easy to understand. :)

Regards,
plusminus
Last edited by plusminus on Sun Feb 03, 2008 3:35 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramgraph1 » Sun Feb 03, 2008 3:11 pm

Wow, you're fast! I was just editing my post as you were replying! Thanks! I'll play with this and see if I can get it working.
ramgraph1
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Wed Jan 09, 2008 10:03 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 22 guests