basteez
basteez

Reputation: 477

Android Wear MessageAPI - can't send messages on UI Thread

I'm following the android wear documentation to send messages from one device to another (https://developer.android.com/training/wearables/data-layer/messages.html)

but I think there's some error in the examples because the send message method throws an IllegalStateException with the following message: await must not be called on the UI thread

How can I fix it?

Upvotes: 2

Views: 1737

Answers (2)

Mariux
Mariux

Reputation: 494

You can't do that because await() blocks the thread.

You want to do this using an asynchronous thread, using for example an AsyncTask, like suggested in the Google Play Services documentation here: https://developer.android.com/google/auth/api-client.html#Sync

And yes, the documentation should be updated to explicate to do tha same for the Wear Api.

Upvotes: 1

gatlingxyz
gatlingxyz

Reputation: 753

Instead of calling .await(), use .setResultCallback(). For example, ...

result.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
            @Override
            public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                Log.v(TAG, "Sent message");
            }
        });

Upvotes: 5

Related Questions