看起来强制停止会阻止应用程序运行,它甚至会禁用所有应用程序的警报.然而,我发现Google日历中的通知即使在强制停止后仍然显示正常,我一直看到Instagram应用程序运行,即使我杀了它,它只是自动重启,它再一次在那里.
那么,什么是让应用程序不断运行的方法?我正在使用提醒进行应用,并且需要在特定时间显示通知,无论之前的应用是如何关闭的.
我已经阅读了关于这个主题的每个Stackoverflow答案,但它们都没有奏效.
问题:每当我的设备处于睡眠模式一小时或更长时间时,服务就会被终止
返回START_STICKY从onStartCommand()使用startForeground()
public int onStartCommand(Intent intent, int flags, int startId) {
notification = makeStickyNotification(); //I've simplified the irrelevant code, obviously this would be a real notification I build
startForeground(1234, notification);
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)这样工作正常,它甚至可以在设备内存不足时重新启动我的服务,但这还不足以解决我的设备暂停一段时间后出现的问题.
在onCreate()我的Activity和onStartCommand()我的服务中使用Alarm Manager 来调用调用我的服务的广播接收器
Intent ll24 = new Intent(this, AlarmReceiver.class);
PendingIntent recurringLl24 = PendingIntent.getBroadcast(this, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*60, recurringLl24); // Every minute
Run Code Online (Sandbox Code Playgroud)这有助于保持我的服务活跃,但同样,不能解决我的问题
使用Schedule Task Executor使其保持活动状态
if (scheduleTaskExecutor …Run Code Online (Sandbox Code Playgroud)