Android 传感器转储响应能力
我在 Android 中有一个应用程序,它将传感器值转储到手机的 SD 卡内存中,并在我按下屏幕上的按钮时自行终止。我正在使用 Sensor_delay_fastest 来获得传感器的最大更新。
我的问题是,由于我在 UI 线程上进行所有计算,因此该按钮需要很长时间才能响应,这对我的应用程序非常不利,因为它会在我按下 Kill 按钮后记录所有额外的值。我知道我应该将计算放在另一个线程中,但我不确定如何针对事件处理操作执行此操作。例如,我有
protected void onResume()
{
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}
onStop() 以及 onSensorChanged() 和 onAccuracyChanged() 函数,它们在事件触发时执行。实际上,主要的“繁重”计算是在 onSensorChanged() 中,我将值写入 SD 卡上,这可能会导致延迟。但我不能将它放在不同的线程中,因为随着事件不断触发,它会创建很多线程。
那么如何让这个系统响应更快呢?我对 Android 非常陌生,非常感谢通过一些示例代码提供的帮助。
谢谢大家!
编辑:我可以让线程实现一个接口,例如 SensorEventListener,然后在该线程中实现 OnResume 和 OnStop 吗?
I have an application in Android that dumps the sensor values to the SDcard memory of the phone and kills itself when I press a button on the screen. I am using Sensor_delay_fastest to get maximum update of the sensors.
My problem is that since I'm doing all my computation on the UI thread, the button takes very long to respond, which is very bad for my application as it records all the extra values after I press the Kill button. I understand that I should put my computations in another thread, but I'm not sure how I would do that for event handling operations. For instance, I have
protected void onResume()
{
super.onResume();
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
}
and onStop(), and the functions onSensorChanged() and onAccuracyChanged(), which are executed when the event fires. Actually, the main 'heavy' computation is in my onSensorChanged(), where I write the values on the SDcard, which is probably causing the delay. But I can't put it in a different thread because then it'll create a LOT of threads as the events keep firing.
So how do I make this system more responsive? I'm really new to Android, and some help by means of some sample code will be greatly appreciated.
Thanks all!
EDIT: Can I make a thread implement an interface, like SensorEventListener and then implmeent OnResume and OnStop within that thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也喜欢将传感器放在不同的线程中。为此,我在为管理器传递上下文时创建了一个 HandlerThread :
然后我在其 Looper 启动后:
从此时起,我们有了一个新线程,我们可以通过其处理程序发送消息。
现在的技巧是使用 SensorManager.registerListener() 以及在其中计算数据的处理程序对象。
从该接口(可以由 SensorThread 本身实现)中,您只需将 SensorEvents 或其他内容传递给 ThreadHandler。
请确保您取消注册侦听器或通过调用 sdt.quit() 停止线程,否则它会耗尽您的电池。
I like to get the sensors in a different Thread, too. For this purpose I created a HandlerThread while passing a Context for the Managers:
then I register a Handler Object on its Looper after starting it:
from this point we have a new Thread which we can send Messages through its Handler.
the trick is now to use the SensorManager.registerListener() with a Handler Object in which the data is computed.
from that Interface (which can be implemented by SensorThread itself) you just pass the SensorEvents or whatever to the ThreadHandler.
be shure you either unregister the Listeners or stop the Thread by calling sdt.quit() otherwise it will drain your battery empty.
您可以在内存中对一堆传感器更改进行排队,然后仅在达到一定数量的数据或用户按下按钮时才写入 SD 卡。减少写入 SD 卡的次数可能会使其响应速度更快。
You could queue up a bunch of sensor changes in memory and then only write to the sd card when you reach a certain amount of data or the user pushes the button. Reducing the number of times you write to the SD Card would probably make it much more responsive.