我应该如何去在这种情况下,越来越语境? [英] How should I go about getting Context in this case?

查看:255
本文介绍了我应该如何去在这种情况下,越来越语境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设计的传感器顶部的传感器管理类像加速指南针等,该项目将是将被引用,并在需要的传感器设备的数据其他Android项目中使用的类库项目。问题是,为了与设备,如加速播放和指南针在我的传感器管理类,我需要上下文。我想不通我怎么能访问当前的情况下在这个场景中。

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?

更新

这是我目前的设计...

This is my current design...

我已经实现单对的SensorManager,也为里面的SensorManager每个设备。因此,例如,将有加速度计内的SensorManager只有一个实例。来电者将得到的SensorManager,然后调用方法RegisterForAccelerometer。由于每当会有任何加速计更新的SensorManager的加速度实例中协调的结果,所有注册听众将得到通知。现在,当我尝试启动监听首次加速度计的坐标,因为我需要背景下问题就来了。

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.

推荐答案

一个方面是不够的你。取决于具体的实现,需要您可能希望存储环境在地图或只使用一个特定的动作,所以这将影响传递这种情况下的方法和时机。

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.

这篇关于我应该如何去在这种情况下,越来越语境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆