问题描述
我正在编写一个自定义 Android Wear 应用程序,该应用程序应该向连接的主机设备(电话)发送一次性消息.通过 API 挖掘,我发现以下教程应该运行良好:http://developer.android.com/training/wearables/data-layer/messages.html
I'm writing a custom Android Wear application that's supposed to fire a one-off message to the connected host device (the phone). Digging through the API, I found the following tutorial that should work well: http://developer.android.com/training/wearables/data-layer/messages.html
我的 Android 应用有一个 WearableListenerService,我的 Android Wear 应用使用 Message API 发送消息.根据记录以下方法连接模拟器时,会调用 WearableListenerService,因此我很确定该服务连接良好
My Android app has a WearableListenerService and my Android Wear app fires off messages using the Message API. The WearableListenerService get's called when the emulator gets connected based on logging the following method, so I'm pretty sure the service is wired up fine
@Override
public void onPeerConnected(Node peer) {
super.onPeerConnected(peer);
String id = peer.getId();
String name = peer.getDisplayName();
Log.d(LOG_TAG, "Connected peer name & ID: " + name + "|" + id);
}
日志输出:
/AndroidWearListenerService(19892): Connected peer name & ID: facdc219-37f5-4326-8fa6-1c8b8d3b6669|facdc219-37f5-4326-8fa6-1c8b8d3b6669
然而,onMessageReceived 方法永远不会被触发:
However, the onMessageReceived method never gets triggered:
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(LOG_TAG, "MessageEvent received: " + messageEvent.getData());
//do work
}
这是我的 Android Wear 代码.我已经删除了大部分样板代码,只留下了必要的位
Here's my Android Wear code. I've removed most of the boiler plate code leaving only the necessary bits
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
googleApiClient.connect();
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
fireMessage();
}
private void fireMessage() {
// Send the RPC
PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
for (int i = 0; i < result.getNodes().size(); i++) {
Node node = result.getNodes().get(i);
String nName = node.getDisplayName();
String nId = node.getId();
Log.d(TAG, "Node name and ID: " + nName + " | " + nId);
Wearable.MessageApi.addListener(googleApiClient, new MessageApi.MessageListener() {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d(TAG, "Message received: " + messageEvent);
}
});
PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
PATH, null);
messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
Status status = sendMessageResult.getStatus();
Log.d(TAG, "Status: " + status.toString());
if (status.getStatusCode() != WearableStatusCodes.SUCCESS) {
alertButton.setProgress(-1);
label.setText("Tap to retry. Alert not sent :(");
}
}
});
}
}
});
}
});
}
日志似乎表明消息已成功发送,但 Android 应用的 WearableListenerService.onMessageReceived 从未触发.
Logging seems to indicate the message was sent successfully, but the Android app's WearableListenerService.onMessageReceived never fires.
D/MyWearApp MyActivity( 2014): Node name and ID: a2ba665d-a559-4a95-91d2-c16fc7873e28 | a2ba665d-a559-4a95-91d2-c16fc7873e28
D/MyWearApp MyActivity( 2014): Status: Status{statusCode=SUCCESS, resolution=null}
有什么想法吗?
推荐答案
您是否确保两个应用的applicationId"相同,即 Android Wear 上的应用和应用打电话?
Did you ensure that the "applicationId" is the same for both apps, i.e. for the app on the Android Wear and the app on the phone?
这篇关于从 Android Wear 向主机设备发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!