Android Start_redeliver_intent takes a long time (hours) to restart service

拟墨画扇 提交于 2020-02-21 15:36:24

问题


I'm currently making an app that polls gps data and uploads it to a server. The service starts when the app is in the foreground, and keeps running when the app enters the background. When the app is swiped close, onTaskRemoved is called and an Alarm Manager restarts the service. The service is an IntentService with an AsyncTask to post the gps coords.

Here is where the weirdness happens. The longer the app stays in the foreground, the longer it takes Android to restart it using START_REDELIVER_INTENT as the return in onStartCommand. The service is very rarely killed by Android, but when it does, it can take a long time to restart itself depending on how long it was in the foreground.(?)..i think.

So for testing purposes, I deleted the alarm manager that restarts the app, so when I swipe the app closed, android will restart it using START_REDELIVER_INTENT. If I have the app open for 30 seconds, meaning the service in the background is running for 30 seconds, and then swipe app closed, Android will restart in under a minute.

But if I start the app, which starts the service in the background, and leave the app in the foreground for lets say 10 minutes, it can take Android over an hour to restart the service when I swipe the app closed. During this 10 minutes, the app is polling gps every 30 secs, and using an AsyncTask to upload coords every 5 mins. I have no idea what is going on. I'm not even entirely sure it's related to the App being in the foreground but i can replicate it over and over by timing how long i keep app in foreground, which seems proportional to how long it takes Android to restart the service.

Man I know this sounds crazy, but I hope it isn't.

Also as a side note, I'm using Cordova/javascript for the app itself, but the service is all Java. Here is my onCreate and onStartCommand. Not sure what else I should post.

@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;
}

来源:https://stackoverflow.com/questions/36188851/android-start-redeliver-intent-takes-a-long-time-hours-to-restart-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!