Android wearables: How to handle the event of a connected device?

久未见 提交于 2019-12-24 14:41:05

问题


From the smartwatch I'd like to intercept the event of the connection to a smartphone. This is automatically managed by Android and Android Wear so I need to get this event from my app running on the watch.

I found out this on WearableAPI to be notified without polling Wearable.NodeApi.getConnectedNodes

public class MyService extends WearableListenerService {
  @Override
  public void onPeerConnected(Node peer) {
    Log.i("my wear service", "connected");
    super.onPeerConnected(peer);
    // here I would use MessageAPI to send data saved on "disk" to the smartphone
  }
}

Is it the right way? So is this a normal Service? Do I have to start it or is it automatically running in background? Are there any other ways to perform this task?

this is the reference


回答1:


to exchange data between the wearable and the handheld app I strongly suggest you to use the Wearable.DataApi or the Wearable.MessageApi. You will probably want to to have a subclass of WearableListenerService running on both side, and handling the communication onDataChanged/onMessageReceived. They have an empty implementation on the super class. So you have to override the one you need. If you use the DataApi you'll have to override onDataChanged, onMessageReceived otherwise.

Do I have to start it or is it automatically running in background

you have to declare your subclass in the manifest, and use BIND_LISTENER as action,

<intent-filter>
   <action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>

Android takes care of the rest.



来源:https://stackoverflow.com/questions/31312265/android-wearables-how-to-handle-the-event-of-a-connected-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!