How can I schedule a specific thread in Blackberry

I want to automatically assign a thread at a specific time interval. I also have to do this in the background continuously without hanging the device.

I tried this with the Application Manager class, but to schedule applications and I need to schedule a thread in the application.

+5
source share
4 answers

I would use TimerTask :

public class MyScreen extends MainScreen {
    private Timer mTimer;
    public MyScreen() {        
        mTimer = new Timer();
        //start after 1 second, repeat every 5 second
        mTimer.schedule(mTimerTask, 0, 5000);
    }

    TimerTask mTimerTask = new TimerTask() {
        public void run() {
            // some processing here
        }
    };
}

see BlackBerry Hidden Jewels API (Part Two)

+6
source

UiApplication.getUiApplication().invokeLater() , .

, , , , , , :

//Start repeating "runnable" thread every 10 seconds and save the event ID
int eventId = UiApplication.getUiApplication().invokeLater(runnable, 10000, true);

//Cancel the repetition by the saved ID
UiApplication.getUiApplication().cancelInvokeLater(eventId);
+2

, : . UiApplication Application main() , . Thread.sleep enterEventDispatcher.

"": http://docs.blackberry.com/en/developers/deliverables/1076/development.pdf

- , "" , , . onClose() Application.getActivation(). Deactivate(), .

- , invokeLater .. , eventlisteners , , .

+1

- Thread.sleep , , .

If you need to wake up at a certain time, and not just sleep for a certain time, you can do something like the following:

Date wakeUpAt = ...; // Get this however
Date now = new Date();
long millisToSleepFor = wakeUpAt.getTime() - now.getTime();
Thread.sleep(millisToSleepFor);
0
source

All Articles