且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以创建一个新的SensorEvent对象来测试onSensorChanged()函数吗?

更新时间:2023-11-23 21:23:40

我认为这不会给您带来任何好处. AFAIK,android.hardware.SensorEvent和com.sun.j3d.utils.behaviors.sensor.SensorEvent之间没有任何关系.

I don't think this will do you any good. AFAIK, there's no relationship between android.hardware.SensorEvent and com.sun.j3d.utils.behaviors.sensor.SensorEvent.

从Android源代码的简要介绍看,似乎根本无法创建自己的SensorEvent对象.如果您问我,这是对Google的严重疏忽.

From a brief look at the Android source code, it looks like there's simply no way to create your own SensorEvent object. This is a serious oversight on Google's part, if you ask me.

这是我的工作.我编写了一个名为sensorChanged(Sensor sensor,float [] values)的方法来完成所有工作,并仅从常规onSensorChanged()方法中调用它.然后,当我想从我的应用程序中测试传感器处理时,我用所需的任何值调用sensorChanged().我可能无法创建SensorEvent对象,但是通过这种方式,我仍然可以测试我的代码.

Here's what I do. I write a method named sensorChanged(Sensor sensor, float[] values) to do all the work, and just call it from the regular onSensorChanged() method. Then, when I want to test sensor handling from within my app, I call sensorChanged() with whatever values I want. I might not be able to create a SensorEvent object, but this way I can still test my code.