我建立Android平台的一个应用,我想用加速度计。现在,我已经找到了传感器模拟一个非常好的应用程序( OpenIntents软件SensorSimulator ),但是,为了什么我想做的事,一想创建自己的传感器模拟器应用程序。

如何做到这一点

我还没有找到的信息(我不知道,如果拆卸模拟器的罐子是正确的),正如我说的,我想建立一个传感器模拟器的更小和更简单的版本,更适合我的意图。

你知道我能开始?我在哪里可以看到什么是代码,我需要建立残局?

基本上,所有我的要求只是一些方向。

有帮助吗?

解决方案

好,看来你想什么,而在模拟器上测试将模拟的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,这次通过套接字到一个桌面应用程序,您将创建在其中输入传感器值或东西通信:

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的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top