I am creating an app where I will send the data I receive from the Android phones sensors, to another Android phone using Wifi-Direct.
I am able to get the values of the sensors by writing the code in a class that extends Activity. I have used the Sensor Manager Class.
But now I want to get the sensor values in a Background thread. Is there any way to do that?
You can listen for the
ACTION_SCREEN_OFFevent inside of anAsync_Taskwhich is great for background threads.When the event fires inside of your
Async_Task, unregister your sensor listener and then re-register it. The screen will continue to stay off, but the monitoring will continue.Here is a relevant article and good code example on this blog
AsyncTask on Google