Java Android AlarmManager.setExact()未启动

Java Android AlarmManager.setExact()未启动,java,android,android-intent,broadcastreceiver,alarmmanager,Java,Android,Android Intent,Broadcastreceiver,Alarmmanager,我在将来使用Alarm Manager触发未决意图时遇到问题。我已经做了几个小时了,不明白我做错了什么。任何帮助都将不胜感激 这项功能可以立即发送广播: _context.startService(notificationIntent); 这样做可以在约30秒内发送广播: if (mgr != null) mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 30000,

我在将来使用Alarm Manager触发未决意图时遇到问题。我已经做了几个小时了,不明白我做错了什么。任何帮助都将不胜感激

这项功能可以立即发送广播:

_context.startService(notificationIntent);
这样做可以在约30秒内发送广播:

if (mgr != null) 
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
if (mgr != null) 
mgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
这样做可以在约30秒内发送广播:

if (mgr != null) 
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime() + 30000, AlarmManager.INTERVAL_DAY * 7, pendingNotificationIntent);
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);

就像它不喜欢我的触发时间…但我知道它是以毫秒为单位的,而且是在将来。谢谢你的输入!我试过你的建议,但仍然没有成功。我在控制台上打印了我想要使用的历元时间…然后我将其转换为秒数,以便我可以使用您的示例:在(历元)1521154500000处调度通知。秒直到触发:38我等了2分钟,我的接收器没有收到广播。令人沮丧的是,我可以立即发送,但不能使用AlarmManager。在我的用例中,我还将.getService更改为.getBroadcast。您是否可以尝试制作一个简单的服务(或者IntentService更好),将消息广播给您的接收者?接收方必须检测该服务何时发送该消息。接收器(据我记忆所及)就像一个被动的“服务”,它听到所有由AndroidManifest.xml中指定的意图过滤器过滤的广播消息。我不认为一定要把它放在每N秒发射一次的悬挂式帐篷里。我犯了一个完全的新手错误,我的朋友。我把这个警报安排在一个循环中,每次迭代都有一个新的触发时间……但是相同的请求id。增加id解决了这个问题。我非常感谢你的意见。