سؤال

I use an alarm to take pictures at regular time intervals. I use a BroadcastReceiver as follows:

@Override
public void onReceive(Context context, Intent intent) 
{
    Log.d(TAG, "Capturing pic");

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

    capturePicture();

    wl.release();
}

The capturePicture() calls Camera.takePicture(...) and then ends. Finally, as you can see, wl.release() is invoked.

Now, the problem is that the call back of the takePicture is very time consuming in my case. In fact, it performs some manipulations on the picture just taken that can take up to 5 seconds on my device.

My question is, since wl.release() is called while the computational intensive task into the takePicture callback is running, is there any side effect? That is, suppose that the device is in standby mode. The alarm starts --> wakelock is activated --> picture is taken and manipulated but in the meanwhile of the computation wakelock is released... is it safe or there is the risk that the device returns to its standby mode before the routines into the takePicture callback ends?

هل كانت مفيدة؟

المحلول

Your setup is wrong in that you should not be doing a lot in the receiver and in that the AlarmManager holds a wakelock while onReceive() runs anyway - and yes you should not release the wakelock while editing. You should delegate to a service and for reasons detailed here this must be a WakefulIntentService. Now in that service you should set up a mechanism for waiting for the processing to finish - maybe using CountDownLatch.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top