简体   繁体   中英

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.

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.

What is the most optimal way to do that without creating a new AsyncTask object for every new accelerometer position?

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. Remember that you set the accelerometer refresh rate for the listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM