好吧,看起來你想做的是一個應用程序,它將在模擬器上進行測試時,在您的應用程序上模擬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