java - Android AlarmManager.setExact() 未触发

标签 java android android-intent broadcastreceiver alarmmanager

我在将来使用警报管理器触发待处理 Intent 时遇到问题。我已经研究了几个小时,但不明白我做错了什么。任何帮助将不胜感激。

这有效,立即发送广播:

_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”(长整型)打印到控制台,这绝对是一个有效时间(根据 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);

关于java - Android AlarmManager.setExact() 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308474/

相关文章:

java - PMD规则在java中查找注解

java - android 上网,导致 twitter4j 异常?

android - 使用 Intent 发送 HTML 电子邮件

java - 如何使用 Intent 打开电子邮件应用程序

android - android中edittext中的电话号码格式

android - 尝试启动 Twitter 应用程序时出现 ActivityNotFoundException

Java 和 REST Web 服务 - GET 方法返回 JSONObject ["....."] 不是 JSONObject

java - 如何重定向 Groovy 脚本的输出?

JAVA循环问题

java - Android应用程序在M中置于 "standby"模式需要多长时间?