Android Start_redeliver_intent需要很长时间(小时)才能重新启动服务

我目前正在制作一个应用程序来轮询gps数据并将其上传到服务器。 该应用程序位于前台时启动该服务,并在应用程序进入后台时继续运行。 当应用程序快速关闭时,将调用onTaskRemoved并且Alarm Manager将重新启动该服务。 该服务是一个带有AsyncTask的IntentService来发布gps coords。

这就是奇怪发生的地方。 应用程序停留在前台的时间越长,Android使用START_REDELIVER_INTENT作为onStartCommand中的返回值重启它所需的时间就越长。 这项服务很少被Android杀死,但是当它发生时,它可能需要很长时间才能重新启动,具体取决于它在前台的时间长度。(?)..我认为。

因此,出于测试目的,我删除了重启应用程序的警报管理器,因此当我将应用程序关闭时,android将使用START_REDELIVER_INTENT重新启动它。 如果我让应用程序打开30秒,意味着后台服务运行30秒,然后滑动应用程序关闭,Android将在一分钟内重新启动。

但是,如果我启动应用程序,它在后台启动服务,并将应用程序保留在前台,比如10分钟,那么当我将应用程序关闭时,Android可能需要一个多小时才能重新启动服务。 在这10分钟内,应用程序每隔30秒轮询gps,并使用AsyncTask每5分钟上传一次coords。 我不知道发生了什么事。 我甚至不完全确定它与应用程序处于前台有关,但我可以通过计时将应用程序保持在前台的时间来反复复制它,这似乎与Android重启服务所需的时间成正比。

男人我知道这听起来很疯狂,但我希望不是。

另外作为旁注,我正在为应用程序本身使用Cordova / javascript,但该服务都是Java。 这是我的onCreate和onStartCommand。 不知道我还应该发布什么。

@Override public void onCreate() { super.onCreate(); //Log.i(TAG, "OnCreate"); locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this); registerReceiver(sendHTMLReceiver, new IntentFilter("html_alarm")); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { isCheckedIn = intent.getExtras().getBoolean("checkedIn"); showNotification("Courtesy Checkout Enabled"); } return START_REDELIVER_INTENT; }