I want to use AsyncTask to open the camera as suggested here http://developer.android.com/reference/android/hardware/Camera.html#open%28int%29:
Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.
So something like:
onResume(){
new AsyncTask(){
doItInBackground(){
camera_ref = Camera.open();
}
}.execute();
}
then in on Pause() I must release the camera:
onPause(){ if(camera_ref!=null) camera_ref.release(); }
Question: What can I do to be sure that open() will never be called after release()? Putting open() inside a synchronized block with a boolean variable shouldn't be a solution since this would still block the UI thread.
In general this problem applies to every situation where I call camera_ref.something() inside a callback method...since I got to be sure this call happens only between open() and release() while the callback method can be called from the UI thread in every moment.
Probably I'm missing something. I'd like to see the way it should be done.
EDIT: As of now, I think the best solution could be to use an IntentService as described here: http://developer.android.com/guide/components/services.html. In particular:
The IntentService does the following:
- Creates a default worker thread.
- Creates a work queue that passes one intent at a time.
That is what I was looking for. Using AsyncTask.executeOnExector(SINGLE_EXECUTOR) is not viable since the AsyncTasks may be killed before even starting. I'll take a further look at ThreadPoolExecutor to see if it may be a possible alternative.