简体   繁体   中英

Android Wear- Getting Data from WearableListenerService

I'm working on an Android wear app that receives data from the mobile device app every x seconds. For receiving the data, I'm using the MessageApi:

Listener in the wearable app:

public class MWearableListenerService extends WearableListenerService {
    private String m_lastMessage;    

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);

        m_lastMessage = new String(messageEvent.getData());
    }
}

I would like my wearable app UI to access the data. I know I can use the GoogleApiClient to send data locally but I would rather avoid that.

What's the best practice for this?

Thanks

As you can read in docs, you have 2 approaches:
MessageApi.MessageListener via listner in your activity or WearableListenerService.

Applications expecting to be notified of events while in the background should extend this class. Activities and other short-lived Android components may use addListener(GoogleApiClient, DataApi.DataListener), addListener(GoogleApiClient, MessageApi.MessageListener) and addListener(GoogleApiClient, NodeApi.NodeListener) to receive events for a limited period of time.

this is the key difference. So you should decide what is your case and chose proper api.

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