I get time with

Date today = new Date(); SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm"); String date = DATE_FORMAT.format(today); 

and send to server. The server processes them.

The problem is that the service sometimes stops sending data. The service falls asleep for 2 minutes and then wakes up by itself.

How to be? I added a shutdown of optimization to my application in the battery menu, but still the service goes to sleep and wakes itself up. Yes, and on charging the service does not fall asleep. That is, I connect the phone to the laptop, I want to see what the problem is through monitoring (the service works as it should). I think this is due to the fact that it is connected to the laptop and is charging. But it did not help me to understand the problem (I’ve added the application to the list of those that were turned off from optimization).

How to understand this behavior?

  • one
    Or maybe instead of a stream of consciousness, you clearly write the problem? - Alexey Shimansky
  • one
    I receive the phone time in the application and send data to the database. Everything is done in the service. After the phone is locked and the screen is turned off, the service does not work correctly. It stops sending data. At intervals of up to 5 minutes. - elik
  • 2
  • 2
    Thank you flipii. Thank you for the tip - elik
  • one
    Remove the cons. - Flippy

1 answer 1

Here is the solution in English SO ->

This code will help the service to work when the screen is locked.

 PowerManager mgr = (PowerManager)context.getSystemService(Context.POWER_SERVICE); WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); wakeLock.acquire(); 

For update

 wakeLock.release(); 

release must be called when the screen is locked. acquire - when unlocked

  • thanks, I understood everything except the update point - elik
  • @elik, added an answer - Flippy
  • Thanks, this is what I need! - elik
  • @elik, no reason :) - Flippy