鉴于 SensorEvent 构造函数不可见,将数据发送到 onSensorChanged
我正在尝试编写一个服务来测试一个实现 onSensorChanged(SensorEvent event)
的类,方法是传递我可以生成的数据,就好像它来自传感器一样。我遇到的问题是我无法创建自己的 SensorEvent 对象来传递函数,因为 SensorEvent 没有公共构造函数。我尝试创建自己的类,该类将具有 SensorEvent 所需的功能并对其进行强制转换,但后来发现为了能够强制转换,它需要是一个子类,并且要对其进行子类化,我需要能够访问构造函数。
是否有其他方法来创建一个可以代替 SensorEvent 传递的对象,或者是否有更好的方法来模拟缺失的功能?
I'm trying to write a service to test a class which implements onSensorChanged(SensorEvent event)
by passing it data I can generate as if it were coming from the sensor. The problem I've run into is that I cannot create my own SensorEvent objects to pass the function as SensorEvent has no public constructors. I tried creating my own class which would have the needed functionality of SensorEvent and casting it but have since discovered that to be able to cast, it would need to be a sublcass and to subclass it I'd need to be able to access the constructor.
Is there any other way to create an object which I can pass in place of the SensorEvent or is there a better approach to simulating the missing functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SensorSimulator 项目可能会执行您想要执行的测试。但如果没有,它应该有一些可能有帮助的代码
The SensorSimulator project probably does the testing you're trying to do. But if not, it should have some code that may help