Android AlarmManager.setExact() 未触发
Android AlarmManager.setExact() not firing
我在将来使用警报管理器触发待定意图时遇到问题。我已经花了好几个小时了,不明白我做错了什么。任何帮助将不胜感激。
有效,立即发送广播:
_context.startService(notificationIntent);
这有效,在 ~30 秒内发送广播:
if (mgr != null)
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
这有效,在 ~30 秒内发送广播:
if (mgr != null)
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
但是由于某些未知原因,这样做失败了。永远不会触发广播。当我取 System.currentTimeMillis(),并从我的触发器中减去它时......它表明触发器确实在未来:
if (mgr != null)
mgr.setExact(AlarmManager.RTC_WAKEUP, trigger, pendingNotificationIntent);
我正在打印我的变量 'trigger'(输入 long)到控制台,这绝对是一个有效时间(根据 epochconverter.com)。它当前正在打印的值(仅供参考)是 1521144300000,几分钟前就过去了..从来没有触发我的警报;
大部分设置如下:
Intent notificationIntent = new Intent(_context, com.example.example.NotificationReceiver.class)
.setAction(ACTION_SHOW_NOTIFICATION)
.putExtra(EXTRA_NOTIFICATION_TITLE, _title)
.putExtra(EXTRA_NOTIFICATION_BODY, newBody)
.putExtra(EXTRA_NOTIFICATION_TRIGGER_TIME, trigger);
AlarmManager mgr = (AlarmManager) _context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingNotificationIntent = PendingIntent.getBroadcast(_context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "trigger time: " + trigger);
if (mgr != null) mgr.setExact(AlarmManager.RTC_WAKEUP, trigger, pendingNotificationIntent);
我从后端收到触发时间,在每个响应中都显示正确。
这也永远不会触发:
if (mgr != null) mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, trigger, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
根据您正在测试警报的 Android 版本,有一些注意事项,但如果您正在测试 Android 6 或更高版本,请尝试下一个代码:
// Init the Alarm Manager.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Setting the PendingIntent to be fired when alarm triggers.
Intent serviceIntent = new Intent(context.getApplicationContext(), YourService.class);
PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, 0);
// Set the alarm for the next seconds.
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + seconds * 1000, pendingServiceIntent);
我在将来使用警报管理器触发待定意图时遇到问题。我已经花了好几个小时了,不明白我做错了什么。任何帮助将不胜感激。
有效,立即发送广播:
_context.startService(notificationIntent);
这有效,在 ~30 秒内发送广播:
if (mgr != null)
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
这有效,在 ~30 秒内发送广播:
if (mgr != null)
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
但是由于某些未知原因,这样做失败了。永远不会触发广播。当我取 System.currentTimeMillis(),并从我的触发器中减去它时......它表明触发器确实在未来:
if (mgr != null)
mgr.setExact(AlarmManager.RTC_WAKEUP, trigger, pendingNotificationIntent);
我正在打印我的变量 'trigger'(输入 long)到控制台,这绝对是一个有效时间(根据 epochconverter.com)。它当前正在打印的值(仅供参考)是 1521144300000,几分钟前就过去了..从来没有触发我的警报;
大部分设置如下:
Intent notificationIntent = new Intent(_context, com.example.example.NotificationReceiver.class)
.setAction(ACTION_SHOW_NOTIFICATION)
.putExtra(EXTRA_NOTIFICATION_TITLE, _title)
.putExtra(EXTRA_NOTIFICATION_BODY, newBody)
.putExtra(EXTRA_NOTIFICATION_TRIGGER_TIME, trigger);
AlarmManager mgr = (AlarmManager) _context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingNotificationIntent = PendingIntent.getBroadcast(_context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "trigger time: " + trigger);
if (mgr != null) mgr.setExact(AlarmManager.RTC_WAKEUP, trigger, pendingNotificationIntent);
我从后端收到触发时间,在每个响应中都显示正确。
这也永远不会触发:
if (mgr != null) mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, trigger, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
根据您正在测试警报的 Android 版本,有一些注意事项,但如果您正在测试 Android 6 或更高版本,请尝试下一个代码:
// Init the Alarm Manager.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// Setting the PendingIntent to be fired when alarm triggers.
Intent serviceIntent = new Intent(context.getApplicationContext(), YourService.class);
PendingIntent pendingServiceIntent = PendingIntent.getService(context, 0, serviceIntent, 0);
// Set the alarm for the next seconds.
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + seconds * 1000, pendingServiceIntent);