How should I go about getting Context in this case?

╄→гoц情女王★ 提交于 2019-12-12 18:33:43

问题


I have to design 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 sensor devices. Problem is that in order to play with devices like accelerometer and compass in my Sensor Manager class I need Context. I can't figure out how can I access the current context in this scenario.

Should I ask the callers/users of my Sensor Manager to pass the Context in some method's parameter? What if more than one classes or activities would be using my Sensor Manager? In actual I would be using only one Context, will it cause problems?

or is there any simple, secure and reliable way to get current application's context?

Update

This is my current design...

I have implemented singleton for SensorManager and also for each device inside SensorManager. So for example there will be only one instance of Accelerometer inside SensorManager. Caller will get the SensorManager and then call method RegisterForAccelerometer. As a result whenever there will be any update in Accelerometer coordinates inside the Accelerometer instance of SensorManager, all the registered callers will be notified. Now the problem comes when I try to start listening for coordinates of accelerometer for first time as I need context.


回答1:


In my opinion you have to pass the context of that particular Activity which is using those devices. I am assuming that you are not making these classes as Singleton.

Also if you want to make the class as singleton I guess you can pass Application context rather than Activity context, but I am not sure about this if this will work.

Update :

Refer to this for Application context http://developer.android.com/reference/android/app/Application.html.

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

So no matter in which Activity you are you will pass this Application context




回答2:


One context will not be enough for you. Depends on your implementation and needs you might want to store contexts in a map or use only for a specific action, so this will impact the method and timing of passing this context.

I could say roughly that if you use it to construct a new object which will use the context or if you have a singleton, ask for context in this call, if you create multiple instances of your class, pass it in the constructor / factory method.



来源:https://stackoverflow.com/questions/7146778/how-should-i-go-about-getting-context-in-this-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!