首页 文章

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

提问于
浏览
1

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

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

我的下一次尝试是LocalNotification . 也许LocalNotification API documentation不正确,因为使用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 回答

  • 1

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

    振动应该可以工作,但如果它处于完全静音模式,则可能无法为您的设备工作 .

相关问题