好吧,看起来你想做的是一个应用程序,它将在模拟器上进行测试时,在您的应用程序上模拟Android设备上的传感器。
可能在你的应用程序,你有这样一行:
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
为什么不创建一个具有您的SensorManager使用的方法的接口:
interface MySensorManager {
List<Sensor> getSensorList(int type);
... // You will need to add all the methods you use from SensorManager here
}
然后创建的SensorManager一个包装,只需在真实的SensorManager对象上调用这些方法即可:
class MySensorManagerWrapper implements MySensorManager {
SensorManager mSensorManager;
MySensorManagerWrapper(SensorManager sensorManager) {
super();
mSensorManager = sensorManager;
}
List<Sensor> getSensorList(int type) {
return mSensorManager.getSensorList(type_;
}
... // All the methods you have in your MySensorManager interface will need to be defined here - just call the mSensorManager object like in getSensorList()
}
然后创建另一个MySensorManager,即此次通讯通过套接字到桌面应用程序nicates您将创建在其中输入传感器值或东西:
class MyFakeSensorManager implements MySensorManager {
Socket mSocket;
MyFakeSensorManager() throws UnknownHostException, IOException {
super();
// Connect to the desktop over a socket
mSocket = = new Socket("(IP address of your local machine - localhost won't work, that points to localhost of the emulator)", SOME_PORT_NUMBER);
}
List<Sensor> getSensorList(int type) {
// Use the socket you created earlier to communicate to a desktop app
}
... // Again, add all the methods from MySensorManager
}
最后,替换您的第一行:
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
随着新线:
MySensorManager mSensorManager;
if(YOU_WANT_TO_EMULATE_THE_SENSOR_VALUES) {
mSensorManager = new MyFakeSensorManager();
else {
mSensorManager = new MySensorManagerWrapper((SensorManager)getSystemService(SENSOR_SERVICE));
}
现在您可以使用该对象而不是之前使用的SensorManager。
嗨艾萨克!感谢您的回答。这或多或少是我想要建立的,我会试一试,让大家知道它是如何发生的。 =) – Hugo 2009-06-30 16:20:13