android - RTC_WAKEUP 和 BroadcastReceiver 的警报以拦截打盹模式
问题描述
我希望我的 react-native headless JS 在后台运行,每 30 秒执行一次工作。当有条件匹配时,我希望在我的 Android 手机上显示通知警报。在手机打瞌睡之前一切正常。警报作业设法按时触发通知消息,就像我在警报管理器中配置的那样。
直到手机打瞌睡,它才会在只有手机唤醒屏幕时才显示通知。它确实有效,但不是我的预期。屏幕唤醒大约每 4-5 分钟发生一次,而不是每 30 秒发生一次。我什至尝试添加另一个警报作业(2 个警报计划作业),但它仍然无法实现。我的目的是让我的手机在触发 onReceive() 时至少显示暗淡的显示。我错过了什么?或者我应该怎么做?
public class AlarmHelper {
private static final int PI_SELF_SCHEDULED_ALARM = 1000;
private static final int PI_REPEATING_ALARM = 1001;
private final Context context;
private AlarmManager alarmManager;
public AlarmHelper(Context context) {
this.context = context;
alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
public void setNextSelfSchedulingAlarm() {
final long fourtySecondsFromNow = System.currentTimeMillis() + SECONDS.toMillis(30);
Intent exactIntent = new Intent(context, AlarmBroadcastReceiver.class).setAction(SELF_SCHEDULING_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, PI_SELF_SCHEDULED_ALARM, exactIntent, FLAG_UPDATE_CURRENT);
if (SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
} else if (SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
} else {
alarmManager.set(RTC_WAKEUP, fourtySecondsFromNow, pendingIntent);
}
}
public void startRepeatingAlarm() {
final int repeatingIntervalInSeconds = 30;
final long tenSecondsFromNow = System.currentTimeMillis() + SECONDS.toMillis(30);
Intent repeatingIntent = new Intent(context, AlarmBroadcastReceiver.class).setAction(REPEATING_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, PI_REPEATING_ALARM, repeatingIntent, FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(RTC_WAKEUP, tenSecondsFromNow, SECONDS.toMillis(repeatingIntervalInSeconds), pendingIntent);
}
}
&
public class AlarmBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock screenWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "ScreenLock tag from AlarmListener");
try {
screenWakeLock.acquire();
Hawk.init(context).build();
String action = intent.getAction();
if (action == null) {
screenWakeLock.release();
return;
}
if (REPEATING_ALARM.equals(action)) {
} else if (SELF_SCHEDULING_ALARM.equals(action)) {
AlarmHelper alarmHelper = new AlarmHelper(context);
alarmHelper.setNextSelfSchedulingAlarm();
}
Intent intentService = new Intent(context, HeadlessTaskService.class);
context.startService(intentService);
} finally {
screenWakeLock.release();
}
}
}
解决方案
推荐阅读
- heroku - 在 Heroku 中连接 AWS RDS 时找不到驱动程序
- angular - 如何将 http:// 重定向到 https://,如 firebase 托管
- flutter - 标签栏中的抽屉也显示在 sliver 应用栏中
- java - 如何修复此错误“渲染期间引发异常:找不到维度资源匹配值 0x1050050”?
- c# - 我可以先在数据库中导入后使实体模型奇异吗
- python - 异步任务取消
- python - 通过这个小测试用例,我是否有理由继续达到我的每日查询限制?
- html - 使用带方向的宽度
- python - actualSolve raise PulpSolverError("CPLEX_PY: Not Available")
- google-tasks-api - Google Task API 不再显示通过日历上的 API 创建的任务