Reputation: 28384
Quick question ... Using J2ME (CLDC 1.1, MIDP-2.1) is it possible to sleep the Midlet for a period of time (not using threads)... For example:
public class myMidlet extends MIDlet{
public void startApp() {
/* Sleep for 10 seconds */
/* The answer was: */
try {
Thread.sleep(time_ms);
} catch (Exception e) {}
}
...
I don't use Java all that much, and don't want to program threads just for a simple sleep.
Thanks in advance
Answer Summary
My lack of Java knowledge. Examples I saw using Thread.sleep() led me to believe it was only usable in a thread object spawned by the Midlet ... not the midlet itself. I didn't want to have to spool off the midlet logic into a thread to sleep it ... But now I know the midlet runs in the default thread :) Going to find that Java book I never read because I didn't think I would use the language ever
Upvotes: 0
Views: 7986
Reputation: 11
You can try using Object.wait()
, Object.wait(long timeoutValue)
. Although I would not advise you to try and delay the main startApp() / system thread.
Upvotes: 0
Reputation: 32920
I would go for Malcolm's approach since your thread may possibly throw an exception.
[...]and don't want to program threads just[...]
Uh, you'll have a hard time programming J2ME and trying to avoid threaded programming. If your app becomes just a bit more complicated, especially when using network connections you'll have to use threads. Moreover if some operation takes more than 2-3 seconds it's highly advisable to run it in a separate thread, possibly (contemporaneously) notifying the user about the ongoing work.
Btw, what I forgot. I've recently written a J2ME application for a university course. There I've constructed what I called "ExecutableTask" which allowed me to handle threads in a convenient and easy way. If you want to have a look at the source...Unfortunately you cannot browse it online in the Google repository due to some bug of Google's hosting solution (some name of my project my cause this).
Upvotes: 1
Reputation: 41510
I didn't understand whether you mean putting midlet in paused state or just stopping execution for specified time.
If it's the latter, actually I don't undesrtand, why you don't want to use Threads, this is no big deal. You just insert three following lines wherever you need:
try {
Thread.sleep(10000);
} catch (Exception ex) {}
That's all, nothing too complicating.
Upvotes: 6
Reputation: 1994
I don't know the exact answer, but I also don't understand what's the problem with calling static method Thread.sleep(milliseconds) that "Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds" . Do you call this programming threads?
Upvotes: 2