【发布时间】:2021-08-03 09:47:11
【问题描述】:
我的服务无法在 android Oreo 下启动时遇到问题。 我有一个警报经理,他在一天中的特定时间安排服务。我有一个 BootCompleted Broadcast 接收器,如果用户重新启动手机,它会重置警报。
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
new AlarmHelper(context).setAlarm(timeInMillis); // i get timeInMilis from SharedPref
}
}
我有一个 AlarmReceiver,它在警报管理器唤醒时启动服务。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.ACTION_SET_REPETITIVE_ALARM)) {
if(!isMyServiceRunning(context, PostReportsService.class)){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(background);
}else{
context.startService(background);
}
}
}
}
Oreo 上方和下方的设备都到达 context.startService(background)(或 context.startForegroundService(background)),但只有 Oreo 上方的设备才真正启动服务。 这仅在设备重新启动后才会发生,否则该服务适用于 Oreo 之下和之上。有什么具体的方法可以解决这个问题吗?
【问题讨论】:
标签: android service alarmmanager reboot