Android传感器模拟器,如何为Android构建传感器模拟器?

好吧,看起来你想做的是一个应用程序,它将在模拟器上进行测试时,在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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值