0

My app works fine when the device is plugged in the power supply whereas it fails (time to time) when the device is unplugged. I think that the piece of code responsible of this issue is as follows:

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "foo");
    wl.acquire();
foo();
    wl.release();

foo() is a function that invokes a Camera.takePicture() that, as you know, results in a parallel task that takes a couple of seconds to complete. Thus, wl.release() is actually called when the picture has not yet taken. The above code is executed by an alarm which wakeup the device from its standby mode. My question is, is there the risk that the device returns in its standby mode before the picture has been taken due to the fact that wl.release might be called before the picture is taken? Does wl.release() release instantaneously the PARTIAL_WAKE_LOCK or the device remains in its run mode for a while?

Many thanks in advance for any comment.

user2923045
  • 369
  • 2
  • 6
  • 16

1 Answers1

0

My question is, is there the risk that the device returns in its standby mode before the picture has been taken due to the fact that wl.release might be called before the picture is taken?

Yes.

Does wl.release() release instantaneously the PARTIAL_WAKE_LOCK

Yes.

or the device remains in its run mode for a while?

That depends on what else might be holding a WakeLock.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks @CommonsWare for your kind reply. The problem is that I do not know when takePicture() ends. The only solution I see is call `wl.release()` at the end of takePicture callback but this is a dirty solution as I do not find any clear mechanism to access wl from the takePicture callback. Do you have any advice? – user2923045 Jan 17 '14 at 16:18
  • @user2923045: "The problem is that I do not know when takePicture() ends" -- your `PictureCallback` gets called with `onPictureTaken()`. "as I do not find any clear mechanism to access wl from the takePicture callback" -- have your instance of your `PictureCallback` implementation hold onto the `WakeLock` instance. – CommonsWare Jan 17 '14 at 16:22