简体   繁体   English

如何在Blackberry中安排特定线程

[英]How can I schedule a particular thread in Blackberry

I want to auto-schedule a thread with a particular time interval. 我想自动安排具有特定时间间隔的线程。 I also need to execute this in the background continously without hangs to the device. 我还需要在后台连续执行此操作而不会挂起设备。

I have tried this with Application Manager Class but it's for application scheduling and I need to schedule thread within the application. 我已经尝试使用Application Manager Class,但是它用于应用程序调度,我需要在应用程序中安排线程。

I would use TimerTask : 我会使用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 API Hidden Gems (Part Two) BlackBerry API Hidden Gems(第二部分)

use UiApplication.getUiApplication().invokeLater() it accepts a delay and repeat parameters and will perform exactly what you need. 使用UiApplication.getUiApplication().invokeLater()它接受延迟和重复参数,并将完全执行您所需的操作。

EDIT 编辑

I know this post is old but this is by far the best option to schedule repeating events and I would like to add that to stop the scheduled event the following is required: 我知道这篇文章已经过时了,但这是迄今为止安排重复事件的最佳选择,我想补充一点来停止预定事件,需要以下内容:

//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);

Assuming you want it to run a thread on device startup: Create a second project and list it as an alternate entry point. 假设您希望它在设备启动时运行一个线程:创建第二个项目并将其列为备用入口点。 In your UiApplication or Application main(), check the argument passed to the project. 在UiApplication或Application main()中,检查传递给项目的参数。 Do your periodic stuff there via Thread.sleep and don't call enterEventDispatcher. 通过Thread.sleep在那里做周期性的东西,不要调用enterEventDispatcher。

search for "autostart": http://docs.blackberry.com/en/developers/deliverables/1076/development.pdf 搜索“自动启动”: http//docs.blackberry.com/en/developers/deliverables/1076/development.pdf

Or if you want to do something once a user "starts" it, then look into creating a new thread to do your timing stuff. 或者,如果您想在用户“启动”它时执行某些操作,那么请考虑创建一个新线程来执行您的计时工作。 Override your screen's onClose() and use Application.getActivation().deactivate() to throw the screen into the background. 覆盖屏幕的onClose()并使用Application.getActivation()。deactivate()将屏幕投射到背景中。

Or there's a other ways to do something like this like invokeLater, etc. Maybe eventlisteners may do what you need, but you didn't give a lot of details. 或者还有其他方法可以像invokeLater那样做这样的事情。也许eventlisteners可能会做你需要的,但你没有提供很多细节。

As long as the application is running - just create the thread and after each bit of work call Thread.sleep for as long as you need it to stay dormant. 只要应用程序正在运行 - 只需创建线程,并在每次工作后调用Thread.sleep ,只要您需要它保持休眠状态。

If you need it to wake up at a particular time, rather than just sleep for a particular time, then 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM