Android穿戴式:如何处理连接设备的事件? [英] Android wearables: How to handle the event of a connected device?
问题描述
从智能手表,我想拦截连接到智能手机的事件。这是由Android和Android的自动管理的穿,所以我需要从上表运行我的应用程序获取此事件。
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.
我发现这对WearableAPI没有投票Wearable.NodeApi.getConnectedNodes被通知
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?
这是<一个href=\"https://developers.google.com/android/reference/com/google/android/gms/wearable/WearableListenerService\"相对=nofollow>引用
推荐答案
要穿戴式和手持式应用程序,我强烈建议你使用 Wearable.DataApi
之间交换数据或 Wearable.MessageApi
。你可能会想有 WearableListenerService
在两侧运行和处理通信 onDataChanged
/ <$的子类C $ C> onMessageReceived 。它们对超类的空的实现。所以,你必须重写你需要的。如果您使用 DataApi
你必须重写 onDataChanged
, onMessageReceived
其他。
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
您必须声明在清单的子类,并使用 BIND_LISTENER
作为动作,
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的负责剩下的照顾。
Android takes care of the rest.
这篇关于Android穿戴式:如何处理连接设备的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!