【发布时间】:2018-06-12 15:21:09
【问题描述】:
我需要每五分钟通知用户一次,无论应用程序是在前台还是后台运行,也无论用户是否使用智能手机。
我的第一次尝试是使用“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);
});
【问题讨论】:
标签: codenameone