Codename One - 每五分钟向用户通知一次

时间:2018-01-02 23:11:10

标签: codenameone

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

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

我的下一次尝试是LocalNotification。也许a comment不正确,因为使用10作为时间(如在API示例中)会产生运行时异常(java.lang.IllegalArgumentException: Cannot schedule a notification to a past time)。我猜测时间是从时代开始的,所以我写了下面的代码。问题在于它无法正常工作,因为只有在用户使用智能手机时才会将消息通知给用户。我的意思是只有当用户在"至少"之后触摸智能手机时才会播放通知声音。五分钟,但如果用户没有触摸智能手机,则不会播放声音。在启动通知之前,应用程序似乎正在等待用户交互。

更清楚一点:我需要像闹钟那样每隔五分钟就会引起用户注意(声音和/或振动)。

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

1 个答案:

答案 0 :(得分:1)

时间是System.currentTimeMillis(),即绝对时间。但是,当应用程序在后台时,本地通知才能正常工作,而不是在前台时。

如果设备处于完全静音模式,振动应该可以正常工作,但可能无效。