设备上未调用WearableListenerService onMessageReceived [英] WearableListenerService onMessageReceived is not called on device
问题描述
我正在尝试使用 Wearable.MessageApi
从Android Wear应用程序向手机应用程序发送一条简单消息。
I am trying to send a simple message from my Android wear app to my phone app using the Wearable.MessageApi
.
这是我在Wear设备上通过GoogleApiClient进行的 onConnected
回调。
This is my onConnected
callback from GoogleApiClient on the Wear device.
final PendingResult<Status> status = Wearable.DataApi.addListener(googleApiClient, this);
status.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (!status.isSuccess()) {
return;
}
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
for (Node node : nodes.getNodes()) {
System.out.println("Sending message: " + node.getDisplayName());
final MessageApi.SendMessageResult result =
Wearable.MessageApi.sendMessage(googleApiClient, node.getId(),
"request", "12345".getBytes())
.await();
System.out.println("sent: " + result.getStatus().isSuccess());
}
}
});
这在运行时显示以下内容
And this is displaying the following when ran
Sending message: Nexus 6P
sent: true
这是我在应用程序上的注册服务:
And this is my registered service on my app:
public class MyWearableListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Toast.makeText(this, "Received message", Toast.LENGTH_LONG).show();
}
@Override
public void onPeerConnected(Node peer) {
Toast.makeText(this, "Peer connected", Toast.LENGTH_LONG).show();
}
}
我正确地验证了当模拟器连接到我的设备时,会显示对等连接
吐司。我正确地做了端口转发,以便在磨损模拟器上进行调试。我检查了我的appId和包名在我的应用程序和Wear应用程序中是否一致。但是,我从未在设备上收到 onMessageReceived
回调。
I properly verified that the Peer connected
toast is showing up when the emulator is connected to my device. I properly did the port forwarding to debug on wear emulator. I checked that my applicationId and package names are consistent across my app and wear app. However, I never get the onMessageReceived
callback on my device.
任何建议都将不胜感激!我已经整整调试了一天:(
Any suggestions are greatly appreciated! I've been debugging this for a whole day now :(
推荐答案
哦,天哪..我知道了我的问题。我认为applicationId是相同的,但事实证明我从未在Wear模块上设置构建样式,因此这两个applicationId实际上是 com.example.android
和 com.example.android.dev
..
Oh, good god.. I figured out my problem. I THOUGHT the applicationId was the same, but it turned out that I never set up build flavors on the wear module, so the two applicationIds were actually com.example.android
and com.example.android.dev
..
希望这可以帮助遇到我同样问题的其他人: \
Hope this helps other people who ran into the same problem as me :\
这篇关于设备上未调用WearableListenerService onMessageReceived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!