好吧,看起来你想做的是一个应用程序,它将在模拟器上进行测试时,在Android设备上为你的应用程序模拟传感器。
可能在你的应用程序中,你有这样一行:
SensorManager mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);为什么不创建一个具有您从SensorManager使用的方法的界面:
interface MySensorManager {
List getSensorList(int type);
... // You will need to add all the methods you use from SensorManager here
}然后为传感器管理器创建一个包装器,它只是在真正的SensorManager对象上调用这些方法:
class MySensorManagerWrapper implements MySensorManager {
SensorManager mSensorManager;
MySensorManagerWrapper(SensorManager sensorManager) {
super();
mSensorManager = sensorManager;
}
List 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,这次通过套接字与桌面应用程序进行通信,您将在其中创建输入传感器值的位置或其他位置:
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 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。