【问题标题】:Android Wear: Starting a service on HandheldAndroid Wear:在手持设备上启动服务
【发布时间】:2016-01-05 00:30:53
【问题描述】:

我正在构建一个 Wear 应用程序,它将与手持设备上的 WearableListenerService 进行通信。 但是,我想确保应用程序在手表上启动时服务已启动并运行。

我最初的想法是发送意图或广播消息以启动服务。 但是,我一直无法弄清楚如何让手表将其发送到配对的手持设备。

在手表方面:

Intent intent = new Intent();
intent.setAction("my.wearable.CONNECT");
sendBroadcast(intent);

在手持端:

public class WearableBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, WearableService.class);
        context.startService(startServiceIntent);
    }
}

<receiver android:name=".service.wearable.WearableBroadcastReceiver" >
    <intent-filter>
        <action android:name="my.wearable.CONNECT" />
    </intent-filter>
</receiver>

【问题讨论】:

    标签: android wear-os


    【解决方案1】:

    有几件事需要澄清

    1. Android Wear Device和Android Device之间的通信接口如下

      • 数据接口
      • 消息接口

      使用NodeApi获取连接的Node ID。

    2. 您不能从穿戴端发送意图并期望在手机端接收它。

    3. 在手机端,如果您正在扩展WearableListenerService,则此服务由 Android 操作系统自行管理。这意味着,如果服务要接收来自 Wear 的消息或数据,Android 将自动创建服务、服务您的请求并在需要时销毁服务。您不必在这里做任何特别的事情。

    清单中定义的意图(上面复制粘贴) 足以让 Android 操作系统对服务进行上述管理。

    【讨论】:

    • 我有一个扩展 WearableListenerService 的服务,即使应用程序死了,它也会做一些事情,这很棒,但如果手持设备它不能向我的可穿戴设备发送任何东西应用程序未激活。当穿戴应用程序打开时,我会向手持设备发送一条消息,然后我通过DataApi 将我的 api 响应发送到可穿戴设备,但它不会显示在可穿戴设备上。在WearableListenerService 中,我等待GoogleApiClient 连接,然后发送到可穿戴设备。这是正确的方法吗?简单地说,我要求手持设备发出请求,然后从服务中将其发回
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多