Turn on/off flashlight while app is minimized

2019-04-12 22:37发布

问题:

In my application I have an activity and a service. The service continously manages some data. In certain cases the service sends a broadcast request to the activity to turn on flashlight.

This works very well. However it does not work when app is minimized (manually or by incomming call). As soon as I bring the app back to the front it starts to work again.

As I observed the log I saw that the service is still running when app is minimized. However since the activity is not present anymore the camera object cannot be accessed to activate the flashlight. But there is no error in the log.

Why is the flashlight not activated? How can the flashlight be activated when the app is minimized?

回答1:

Only the foreground activity can hold the camera. From the Google documentation:

"If your application does not properly release the camera, all subsequent attempts to access the camera, including those by your own application, will fail and may cause your or other applications to be shut down."

You can try claiming it on the service instead, but I suspect that won't work well. It just really wasn't designed to work this way.