应用程序线程如果被任务管理器杀死则会关闭.需要重新调用应用程序,就像它被其他应用程序或任务管理器杀死一样.任何的想法?
在开发过程中,我想测试系统杀死服务的情况.这是因为我在Android Wear和掌上电脑之间进行通信时失去连接.我认为它与系统杀死一些服务有关.
有没有人建议如何处理这个问题?
我已经阅读了关于这个主题的每个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)我希望每次Intent.ACTION_BATTERY_CHANGED被广播时都会调用Broadcastreceiver,这样我就可以跟踪一些数据.
我现在拥有的:
public class ReceiverApplication extends Application {
@Override
public void onCreate() {
// Register Receiver
}
public static BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Start Service to progress data
}
};
}
Run Code Online (Sandbox Code Playgroud)
这很好用直到我使用最近的应用程序"Taskmanager"来刷掉我的一个偏好屏幕或我的主要活动.然后BroadcastReceiver不再被调用.如果我启动其中一个活动,则调用ReceiverApplication的onCreate()并再次开始工作.
我可以通过一直运行空服务来解决这个问题.通过滑动一个活动似乎没有杀死该服务,并且在一段时间后调用onCreate()并重新启动BroadcastReceiver而不做任何事情.这里的问题是我不希望服务一直运行.
那么,如果用户想要的话,我怎样才能在任何能够接收广播的Acitivity或Service之外获得Broadcastreceiver?