Android Wear - 从没有 WearableListenerService 的 Wear 接收消息? [英] Android Wear - receiving message from Wear without WearableListenerService?

查看:25
本文介绍了Android Wear - 从没有 WearableListenerService 的 Wear 接收消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on an app for Android Wear which can start an activity on the phone. Everything works great, but I'd like to not rely on a constantly running background service on the phone to pick up this message. Ideally, I would have the WearableListenerService start at the same time as the app and stay running until onPause.

有没有办法用广播接收器和意图过滤器拦截来自 Wear 的消息,然后从接收器启动应用程序/服务?部分代码如下:

Is there a way of intercepting a message from Wear with a broadcast receiver and intent filter, and then starting the app/service from the receiver? Here's some of the code:

向手机发送消息:

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 类,所以通信是双向的.

On the receiving end, I have WearableListenerService which receives a message. (Note, both phone and mobile have WearableListenerService classes so communication is 2-way.

推荐答案

根据 发送同步数据训练:

WearableListenerService (用于服务)

WearableListenerService (for services)

扩展 WearableListenerService 可让您侦听服务中的重要数据层事件.系统管理 WearableListenerService 的生命周期,在需要发送数据项或消息时绑定到服务,**在不需要工作时解除绑定服务.*

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 (用于前台活动)

DataListener (for foreground activities)

在活动中实施 DataListener 可让您在活动处于前台时侦听重要的数据层事件.使用它而不是 WearableListenerService 可以让您仅在用户积极使用您的应用时监听更改.

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.

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

If you only want to receive messages when the activity is open (ignoring any messages sent when it is closed), you can add a MessageApi.MessageListener to your activity.

如果你想接收消息,无论你的应用是否打开(确保你不会丢弃从 Wearable 发送的消息),那么你应该使用 WearableListenerService(注意:WearableListenerService 不会持续运行 - 它仅在发送新消息/节点连接/数据层更改时启动,并在没有更多消息时停止).

If you want to receive messages whether your app is open or not (ensuring you don't drop messages sent from the Wearable), then you should use a WearableListenerService (note: WearableListenerService does not run constantly - it is only started when a new message/node connection/data layer change is sent and stopped when there are no more messages).

这篇关于Android Wear - 从没有 WearableListenerService 的 Wear 接收消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆