【问题标题】:Codename One - Notify something to the user every five minutes代号一 - 每五分钟通知用户一次
【发布时间】: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


【解决方案1】:

时间来自System.currentTimeMillis(),即绝对时间。但是本地通知只有在应用处于后台时才能正常工作,而不是在前台时。

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

【讨论】:

  • 我在打开后尝试将应用置于后台,但没有收到任何本地通知。在 Android 4.1 上测试
  • 确保您有足够的时间让后台通知结束。还可以尝试更新版本的 Android,Google 在这里的向后兼容性非常糟糕,而且到目前为止类似的东西往往很不稳定
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-19
  • 2020-11-17
  • 2011-12-22
  • 2021-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多