我需要在一段时间内运行一段代码,即使应用程序被杀死或设备处于睡眠状态。
我正在使用AlarmManager
来实现这一点,但当设备休眠约五分钟时,Service
不再被调用...... 有人知道我做错了什么吗?
这是我的代码:
public class Profiler extends IntentService {
public static final int ALARM_MANAGER_ID = 21436587;
public Profiler() {
super("Profiler");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
JodaTimeAndroid.init(getApplicationContext());
System.out.println("Testing profiles");
List<Profile> profiles = MainActivity.getStoredProfiles(getApplicationContext());
if (profiles != null) {
for (Profile profile : profiles) {
if(profile.check()) {
profile.set(getApplicationContext());
}
}
}
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent2 = new Intent(getApplicationContext(), Profiler.class);
PendingIntent pIntent = PendingIntent.getService(getApplicationContext(), ALARM_MANAGER_ID, intent2, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, DateTime.now().plusSeconds(10).getMillis(), pIntent);
}
}
这是另一个问题。这种调用服务的方法可靠吗?
以下是日志。
07-24 11:31:24.654 17689-19078/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:34.671 17689-20364/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:34.673 17689-20364/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:34.674 17689-20364/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:44.692 17689-21571/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:44.693 17689-21571/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:44.694 17689-21571/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:31:54.704 17689-22753/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:31:54.705 17689-22753/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:31:54.706 17689-22753/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:32:04.717 17689-24015/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:32:04.718 17689-24015/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:32:04.718 17689-24015/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
07-24 11:35:29.032 17689-17475/cz.fjerabek.soundprofiler I/System.out: Testing profiles
07-24 11:35:29.040 17689-17475/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":11,"minute":15}
07-24 11:35:29.050 17689-17475/cz.fjerabek.soundprofiler I/System.out: {"type":"Time","hour":0,"minute":0}
[ 07-24 11:35:29.056 17476:17476 I/ ]
power log dlsym ok
应用程序在11:31启动,现在是11:40,你可以看到代码的最后执行时间是11:35,在那之前是11:32,间隔为3分钟,似乎每5分钟调用一次。
我正在使用以下方式启动服务:
Intent bgServiceIntent = new Intent(getApplicationContext(), Profiler.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if(checkNotificationPolicy(getApplicationContext())) {
startService(bgServiceIntent);
}
} else {
startService(bgServiceIntent);
}
我使用的设备是:华为P9 Lite(VNS-L21)