I need to create a Sensor Manager class on top of sensors like accelerometer , compass , etc. This project will be a class library project that will be referenced and used in other Android projects that need data from the deviceβs sensor. The problem is that to play with devices like accelerometer and compass in my Sensor Manager class, I need Context . I cannot figure out how I can access the current context in this scenario.
Should I ask callers / users of my Sensor Manager to pass Context in some method parameter? What if more than one class or activity uses my sensor manager? In fact, I would use only one Context, will this cause problems?
or is there a simple, safe and reliable way to get the current application context?
Update
This is my current design ...
I implemented a singleton for the SensorManager as well as for each device inside the SensorManager. So, for example, in SensorManager there will be only one instance of the accelerometer. Caller will receive the SensorManager, and then call the RegisterForAccelerometer method. As a result, whenever the Accelerometer coordinates inside the Accelerometer SensorManager instance are updated, all registered subscribers will be notified. Now the problem arises when I try to start listening to the coordinates of the accelerometer the first time I need context.
android android-context
Haris Hasan Aug 22 '11 at 11:27 2011-08-22 11:27
source share