发出每天晚上 9:00 运行的通知

发布于 12-04 11:40 字数 272 浏览 2 评论 0原文

我想在我的应用程序中进行自动同步,该同步将在每天晚上 9:00 进行。我这样做的方式是:

try { Thread.sleep(24hours); } catch (Exception ex) { }

但问题是当用户在 24 小时之前关闭他的设备时,当打开它时线程将再次休眠 24 小时。

所以我正在考虑一种根据系统时钟来管理这个问题的方法。当设备时间为晚上 9:00 时,应用程序将收到警报,我将执行工作。

有可能做到这一点吗?

I want to make an auto synchronize in my application that will happen every day at 9:00 pm. I am doing this in the way of :

try { Thread.sleep(24hours); } catch (Exception ex) { }

But the problem is when the user turns off his device before 24 hours, When turning it on the thread will sleep again for 24 hours.

So I am thinking of a way to manage this depending on the clock of the system. When the device has the time of 9:00pm, the application will be alerted and I will do the work.

Is it possible to make this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦罢2024-12-11 11:40:43

您想要安排您的应用程序在特定时间运行。黑莓的操作系统支持这一点。请参阅安排 BlackBerry 应用程序

You want to schedule your app to run at a particular time. BlackBerry's OS supports this. See Schedule a BlackBerry application

念﹏祤嫣2024-12-11 11:40:43

你看过 Quartz (http://www.quartz-scheduler.org/) 吗?也许它会适用。

Have you taken a look at Quartz (http://www.quartz-scheduler.org/)? Maybe it will be applicable.

一口甜2024-12-11 11:40:43

我建议使用已经制作的可用开源 Java 调度程序之一: 开源作业调度程序列表在Java中

我熟悉的是该列表中的第一个: Quartz

。 .. 看起来有人抢先了我。 (上面的答案是在我写这篇文章时添加的)

但为了真正快速扩展,您可以轻松设置在后台运行的“作业”,与用户分开。现在,我个人将其用于网站,而不是用户可以关闭和打开的设备。话虽如此,我确信您想要做的事情之前已经解决过,您应该尝试一下石英,它可能可以满足您的需要。

祝你好运,
-阿萨夫

I recommend using one of the available, open source java schedulers already made: List of Open Source Job Schedulers in Java

The one I'm familiar with is the first in that list: Quartz

... Looks like someone beat me to it. (The answer above was added while I was writing this one)

But just to expand real quick, you can easily set up "jobs" that run in the background, separate from the user. Now I've personally used this with websites and not with devices that users can turn off and on. That being said I'm sure what you're trying to do has been tackled before and you should give quartz a try, it can probably do what you need.

Good luck,
-Asaf

硪扪都還晓2024-12-11 11:40:43

该问题可以通过运行简单的后台线程来解决。

  public static void main(String[] args) {
        Thread myJobThread = new Thread(this, "My background job");
        myJobThread.setDaemon(true);
        myJobThread.start();
    }

    // check the time every minute
    private int checkRate = 60000;

        @Override
        public void run() {
            while(true) {
                Date now = new Date();
                if (now > 9a.m.) {
                    // do your stuff
                }
                try {
                    Thread.sleep(checkRate);
                } catch (InterruptedException e){
                    // handle ex
                }
            }
        }

The problem might be resolved by running simple background thread.

  public static void main(String[] args) {
        Thread myJobThread = new Thread(this, "My background job");
        myJobThread.setDaemon(true);
        myJobThread.start();
    }

    // check the time every minute
    private int checkRate = 60000;

        @Override
        public void run() {
            while(true) {
                Date now = new Date();
                if (now > 9a.m.) {
                    // do your stuff
                }
                try {
                    Thread.sleep(checkRate);
                } catch (InterruptedException e){
                    // handle ex
                }
            }
        }
澉约2024-12-11 11:40:43

您可以考虑使用 RealTimeClockListener。然后,您将编写一个方法clockUpdated(),操作系统每分钟都会调用该方法。在那里您可以进行计算以确定是否是时候执行您的代码。

You could consider using the RealTimeClockListener. You will then write a method clockUpdated() that will be called by the OS every minute. In there you can do your calculation to determine if it is time to execute your code.

痕至2024-12-11 11:40:43

也许你可以使用日历来做到这一点:

    boolean nine=false;

    //until it's nine'o clock
    while(!nine){

      Calendar calendar=Calendar.getInstance();
        if(calendar.get(Calendar.HOUR_OF_DAY)==9&&calendar.get(Calendar.MINUTE)==0&&calendar.get(Calendar.SECOND)==0&&calendar.get(Calendar.MILLISECOND)==0){
            nine=true;
                nine=true;
            }
            else {
                sleep(yourTime);
            }
    }

    //...whatever should happen now...

   //put the boolean back to false;
    nine=false;

Maybe you can do it with Calendar:

    boolean nine=false;

    //until it's nine'o clock
    while(!nine){

      Calendar calendar=Calendar.getInstance();
        if(calendar.get(Calendar.HOUR_OF_DAY)==9&&calendar.get(Calendar.MINUTE)==0&&calendar.get(Calendar.SECOND)==0&&calendar.get(Calendar.MILLISECOND)==0){
            nine=true;
                nine=true;
            }
            else {
                sleep(yourTime);
            }
    }

    //...whatever should happen now...

   //put the boolean back to false;
    nine=false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文