Android has new updates recently. This is the background execution limit. We can check it out here. So the android tablet will put these services to sleep whenever we press the power button to turn the screen off. Or even the tablet automatically turned off itself.
At first time I encountered this issue, I thought this is about the internet connection. Obviously, the internet is so slow. We have the evidence for that. So I added some fixes for retry mechanism whenever the internet failed. You can see the article here.
I re-installed the software. Ok, it worked with first try. Great! But nex few days, our customers complained that issue happened again.
We haven’t changed the software for a long time. All of sudden, our customers got issues. So I think the changes must be the android version. I have checked it and saw the version was up to 11.
The solution below is not really good but that’s all what we are looking for. The service must continue to run on the background in one hour since the users press the power button. We use the PowerManager. Many developers said that it will damage our battery, not good for it. But we use more, we pay more, don’t we? We want it continue to run on the background and it works like that.
import android.os.PowerManager;
public class BLSyncService extends Service {
private static PowerManager.WakeLock wakeLock;
private static final int ONE_MINUTE = 60000;
private int allowed_minutes = 60;
@Override
public void onDestroy() {
wakeLock.release();
}
@Override
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Secure:MyWakeLock");
formsDB = new DBHelper(this);
startWatching();
PDFBoxResourceLoader.init(getApplicationContext());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
wakeLock.acquire(allowed_minutes * ONE_MINUTE);
return START_STICKY;
}
}
Above is my commit changes. Hopefully it will help you guys who are struggling with this. The solution is simple. The way we found the problem is more important.