代号一-每五分钟通知用户一件事 [英] Codename One - Notify something to the user every five minutes

查看:54
本文介绍了代号一-每五分钟通知用户一件事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论应用程序是在前台运行还是在后台运行,无论用户是否使用智能手机,我都需要每五分钟通知用户一次。

I need to notify something to the user every five minutes, regardless the app is running in foreground or background and regardless the user is using the smartphone or not.

我的第一次尝试是使用 UITimer + Display.getInstance()。vibrate(10000),但是振动方法在我的Android智能手机中没有任何作用。

My first attempt was to use "UITimer" + "Display.getInstance().vibrate(10000)", but the vibrate method doesn't do anything in my Android smartphone.

我的下一个尝试是LocalNotification。也许 LocalNotification API文档不正确,因为使用了10时间(如API示例中的时间)产生运行时异常( java.lang.IllegalArgumentException:无法将通知安排到过去的时间)。我猜想时间已经到了时代,所以我写了下面的代码。问题是它无法按我的需要工作,因为它仅在用户使用智能手机时才将消息通知给用户。我的意思是仅在用户至少五分钟后触摸智能手机时才播放通知声音,但是如果用户没有触摸智能手机,则不会播放声音。似乎该应用在启动通知之前正在等待用户交互。

My next attempt was to the LocalNotification. Maybe the LocalNotification API documentation is not correct, because using 10 as time (like in the API example) produces a runtime exception (java.lang.IllegalArgumentException: Cannot schedule a notification to a past time). I guessed that the time is from the epoch, so I wrote the following code. The problem is that it's not working as I need, because it notifies the message to the user only when the user is using the smartphone. I mean that the notification sound is played only when the user is touching the smartphone after "at least" five minutes, but if the user is not touching the smartphone no sound is played. It seems that the app is waiting for the user interaction before launching the notification.

要更清楚:我需要像闹钟这样的东西来吸引用户的注意(声音和/或振动)。

To be more clear: I need something like an alarm clock that gets the user attention (with a sound and/or a vibration) every five minutes.

Form hi = new Form("Five minutes alert", BoxLayout.y());
        hi.add(new Label("Five minutes alert"));
        hi.show();

        UITimer.timer(1000 * 60 * 5, true, () -> {
            long time = Calendar.getInstance().getTime().getTime() + 1000;

            LocalNotification ln = new LocalNotification();
            ln.setId("LnMessage");
            ln.setAlertTitle("Alert title");
            ln.setAlertBody("Alert message");
            ln.setAlertSound("/notification_sound_bell.mp3");

            Display.getInstance().scheduleLocalNotification(ln, time, LocalNotification.REPEAT_NONE);
        });


推荐答案

时间从 System开始.currentTimeMillis(),即绝对时间。但是本地通知仅在应用程序在后台运行时才正常工作,而在前台运行时则无法正常工作。

The time is from System.currentTimeMillis() i.e. absolute time. But local notification will only work properly when the app is in the background not when it's in the foreground.

Vibrate应该可以工作,但如果在设备中,则可能不适用于您的设备完全静音模式。

Vibrate should work but might not have worked for your device if it's in complete mute mode.

这篇关于代号一-每五分钟通知用户一件事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆