-1

I have a service that uses wakelocks to keep the screen on. Everything works wonderfully while the service is running: I am able to acquire and release the wakelock depending on different functions.

However, when the user turns off the service, I release the wakelock in onDestroy. Although I am calling wakelock.release() , and the service ends, the wakelock continues until the process is killed. I can see that onDestroy has been called with the log, as well as the second log call. What am I doing wrong?

@Override
public void onDestroy() {
    releaseMainWakeLock();
    Log.d(TAG, "ScreenService destroyed");
}

private static void releaseMainWakeLock() {
    if(wakeLockMain.isHeld()) {
        Log.d(TAG, "Wakelock being released!");
        wakeLockMain.release();
    }
}

UPDATE: It looks like NO code is actually successful in onDestroy, even though onDestroy does run (as can be seen by the log call). For example, I have some receivers that are supposed to be unregistered in onDestroy. onDestroy runs, the service is "killed" but remains as a cached background process that continues to do functions of the running service!

Each time I stop and start the service, there is a new set of listeners being registered and never unregistered. For example, I have a proximity listener that logs "Proximity event". After I start and stop the service 3 times, looking at logcat after placing my hand over the proximity sensor shows:

"Proximity event Proximity event Proximity event"

This is even though the service should have stopped and that sensor listener should have been unregistered in onDestroy!

Flyview
  • 1,899
  • 1
  • 28
  • 46
  • Are you sure your service isn't just being restarted? – matiash May 30 '14 at 16:42
  • Yes I'm sure, it isn't. It is no longer in "Running Services" but continues to work. It remains as a "cached background process". If I force stop that, it actually does die (because the process is killed), and everything is released. I'm starting to wonder if it's because a lot of the variables are static in the service? – Flyview May 30 '14 at 16:47
  • Well, I don't want to contradict you, but what you're reporting doesn't match well with the expected behavior. :) I would suppose this to be a much simpler bug. Also, "cached processes" are _not_ running any services. – matiash May 30 '14 at 17:07
  • What's contradicting? Maybe I'm not making myself clear. A process will continue to run even after onDestroy, I know that. You can see these in "Cached background processes". After I call stopService, the process moves from "Running services" to "Cached background processes" and continues to work. Please check the updated question again, I gave one more example. – Flyview May 30 '14 at 17:09
  • I mean, _if cleanup is complete when onDestroy() finishes_, then the service wouldn't be able to produce any visible effect unless it's restarted. Therefore, it's likely that some resources were not actually released. Could you please post an MCVE, or at least more details about your actual code? – matiash May 30 '14 at 17:13
  • Well that's my entire problem, onDestroy doesn't seem to be actually doing anything. If you search around, you will see that even after onDestroy, processes continue to run until Android decides to kill the process. I am okay with this, but not if onDestroy doesn't do what I tell it to. What is MCVE? Thanks for your help! – Flyview May 30 '14 at 17:16
  • [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). In other words, code we can compile and run that produces this effect. :) – matiash May 30 '14 at 17:19
  • @matiash I just figured it out. I was completely mislead. Please see the answer below. I can only accept it tomorrow. – Flyview May 30 '14 at 22:56

1 Answers1

0

I figured it out. I had a runnable thread that re-triggered itself every 5 seconds. Since this runnable was not stopped, it was re-registering the proximity sensor and re-acquiring the wakelocks even after onDestroy released the sensor and released the wakelocks!

Lesson to be learned: Be careful, code of the service does not stop with onDestroy and will keep running as long as the process stays alive!

Flyview
  • 1,899
  • 1
  • 28
  • 46