There is a service, there is a timer in it, the timer updates the SQLite record every minute. But if you turn on the music and block it works, or put on charge and block it works.
1 answer
In general: 1. In the manifesto wrote:
<uses-permission android:name="android.permission.WAKE_LOCK" /> 2. In service:
@Override public int onStartCommand(Intent intent, int flags, int startId) { PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Healths"); wl.acquire(); PendingIntent pi = intent.getParcelableExtra("PendingIntent"); timerTask = new mTimerTask(pi); 3.In the timer:
class mTimerTask extends TimerTask{ PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Healths"); ...<другие действия> @Override public void run() { wl.acquire(); ...<другие действия> As a result, the service does not freeze when the screen is locked. Simply, the OS itself suppresses the service when it is locked, since it wants to save battery power, and in the manner described above, you can prevent the OS from jamming the service. :)
|