简体   繁体   English

如何在Android上通过WiFi流数据而不创建多个AsyncTask?

[英]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.

相关问题 如何在Android中解析多个不同的API URL,而不创建由AsyncTasks类扩展的相同类? - How to parse multiple different API URLs in Android without creating same classes which are extended by AsyncTasks Classes? 如何通过Wifi或Wifi热点跨多个Android设备流媒体(音频/视频)? - How to Stream Media(Audio/Video) Across Multiple Android device over Wifi or Wifi Hotspot? 如何在Android中管理多个AsyncTask - How to manage multiple AsyncTasks in Android Android中的多个异步任务 - Multiple asynctasks in android 通过wifi将视频流传输到android设备 - Stream video to android devices over wifi 如何使用多个AsyncTask - How to use multiple AsyncTasks 如何在没有 USB 连接的情况下通过 WiFi 调试我的 Android 应用程序? - How to Debug my Android Application Over WiFi Without a USB Connection? 如何在没有互联网的情况下通过内部 wifi 分发 Android 应用程序 - How to distribute an Android App over the inhouse wifi without internet Android Spinner上出现多个AsyncTasks问题 - Multiple AsyncTasks issue on Android Spinner 如何在不重叠的情况下对AsyncTasks执行多个相关调用? - How to perform multiple related calls to AsyncTasks without overlapping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM