gpt4 book ai didi

Android Wear - 在没有 WearableListenerService 的情况下接收来自 Wear 的消息?

转载 作者:太空狗 更新时间:2023-10-29 13:22:13 29 4
gpt4 key购买 nike

我正在开发一款适用于 Android Wear 的应用程序,它可以在手机上启动一项 Activity 。一切都很好,但我不想依赖手机上不断运行的后台服务来接收这条消息。理想情况下,我会让 WearableListenerService 与应用程序同时启动并保持运行直到 onPause。

有没有办法使用广播接收器和 Intent 过滤器拦截来自 Wear 的消息,然后从接收器启动应用程序/服务?下面是一些代码:

向手机发送消息:

private void sendMessage(String path, byte[] data) {
if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, mNode.getId(), path, data).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e("TAG", "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
Toast info = Toast.makeText(WearActivity.this, "Failed to send message, status code: " + sendMessageResult.getStatus().getStatusCode(), Toast.LENGTH_SHORT);
info.show();
}
}
}
);
}
}

在接收端,我有接收消息的 WearableListenerService。 (注意,手机和手机都有 WearableListenerService 类,因此通信是双向的。

最佳答案

根据 Sending an Syncing data training :

WearableListenerService (for services)

Extending WearableListenerService lets you listen for important data layer events in a service. The system manages the lifecycle of the WearableListenerService, binding to the service when it needs to send data items or messages and **unbinding the service when no work is needed.*

DataListener (for foreground activities)

Implementing DataListener in an activity lets you listen for important data layer events when an activity is in the foreground. Using this instead of the WearableListenerService lets you listen for changes only when the user is actively using your app.

如果您只想在 Activity 打开时接收消息(忽略关闭时发送的任何消息),您可以添加 MessageApi.MessageListener到你的 Activity 。

如果您希望无论您的应用程序是否打开都接收消息(确保您不会丢弃从可穿戴设备发送的消息),那么您应该使用 WearableListenerService(注意:WearableListenerService 不会持续运行 - 它仅在发送新消息/节点连接/数据层更改时启动,并在没有更多消息时停止)。

关于Android Wear - 在没有 WearableListenerService 的情况下接收来自 Wear 的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27130706/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com