1

I've searched for many answers, posts and articles about getting sensor data even when the screen was off, and most of them suggested to try and use a PARTIAL_WAKE_LOCK. I read the documentation, and from what I've understood, all it said was needed to do was to call:

val wakeLock: PowerManager.WakeLock =
    (getSystemService(Context.POWER_SERVICE) as PowerManager).run {
        newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp::MyWakelockTag").apply {
            acquire()
        }
    }

I did this in the onCreate method of the activity that should trigger the wakelock, and I made sure that when the activity is closed the wakelock is released.

My goal is to let the user turn off the screen while in this activity that has a foreground service:

startService(Intent(this, ForegroundService::class.java))

that picks up data from the linear acceleration sensor. Currently though, the partial wakelock does not seem to do anything. As soon as the screen is turned off, the data from the sensor is stopped (as Android would normally do without a wakelock).

It is important that my foreground service analizes the sensor data continuously (so I don't think sensor batching could work for me), and that the user can turn the screen off (all the code works when the screen is on).

Any ideas on how to make this work?

Edit: The service does not stop when the screen is turned off. All the other tasks that the service has to do are correctly done. And that worked without the PARTIAL_WAKELOCK too. It’s just that Android stops getting sensor updates although I have added the Wakelock. That’s the issue.

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31

0 Answers0