[英]How to stream data over WiFi on Android without creating multiple AsyncTasks?
I need to stream the accelerometer positions from my cellphone to my computer, and I'm doing something like this, for every new accelerometer position. 我需要将加速度计位置从手机流式传输到计算机,并且对于每个新的加速度计位置,我都在做类似的事情。
AsyncTask<String, Void, String> task = new SendUDPTask();
task.execute("192.168.0.101", x + " " + y + " " + z);
Inside the AsyncTask, I'm sending the data via UDP. 在AsyncTask内部,我正在通过UDP发送数据。
I know that you can't execute an AsyncTask more than one time, and I know that creating a new object every time I am sending a position (about 30 positions/second) is probably not the right way. 我知道您不能多次执行AsyncTask,而且我知道每次发送职位(约30个职位/秒)时创建一个新对象可能都不正确。
What is the most optimal way to do that without creating a new AsyncTask object for every new accelerometer position? 没有为每个新的加速度计位置创建新的AsyncTask对象的最佳方法是什么?
It all depends on what you do with sent the positions. 这完全取决于您发送职位的方式。
If you want something close to real-time, you have no choice but to send as soon as you receive an accelerometer event. 如果您想获得接近实时的信息,则别无选择,只能在收到加速度计事件后立即发送。
If real-time does not matter (or doesn't have to be that "real") you can wait until you receive N positions (eg 30) and send them all at once. 如果实时不重要(或不必一定是“真实的”),您可以等到收到N个职位(例如30个)并立即发送所有职位。 Remember that you set the accelerometer refresh rate for the listener.
请记住,您设置了侦听器的加速度计刷新率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.