简体   繁体   中英

How to wait in Android without locking the UI thread?

I have an app which uses nearby connections API. I connect x number of devices with a star strategy, then from a database I query a question on the "server" device, then send it forward to all other devices, but I have to wait till everyone answers the question (when someone answers, they send the answer to the "server" device, and it stores the answer in a list), so i need to wait on every devices till this list contains everyone's answer, then query an other question and so on. How can I achieve the waiting without freezing the UI? (And probably display some message till waiting, for example 3/5 players answered?)

You can use Threads to avoid UI freezing.

Here you have some examples and explanations from the official Android guide: Threads

Hope this helps you!

Instead of making continuous request to the server, it would be better that the server informs you about changes in the data.

For instance, you can achieve this using Push Notifications, integrating FCM (Firebase Cloud Messaging) on your Android app.

This way you can execute code only when needed.

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