Android Wear:在手持设备上启动服务 [英] Android Wear: Starting a service on Handheld

查看:22
本文介绍了Android Wear:在手持设备上启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a Wear app that will communicate with a WearableListenerService on the handheld. However, i want to make sure that service is up and running when the app starts on the watch.

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

My initial thought was either send an intent or a broadcast message to get the service started. However, i've been unable to figure out how to get the watch to send that to the paired handheld instead.

在手表方面:

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>

推荐答案

有几件事需要澄清

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

  1. The communication interfaces between an Android Wear Device and an Android Device are the following

  • 数据接口
  • MessageApi

使用 NodeApi 获取连接的节点 ID.

Use NodeApi to get the connected Node ID.

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

You cannot send a intent from wear side and expect to receive it on the phone side.

在手机方面,如果您要扩展 WearableListenerService,那么此服务由 Android 操作系统自行管理.意思是,如果服务要从 Wear 接收消息或数据,Android 会自动创建服务,为您的请求提供服务并在需要时销毁服务.您不必在这里做任何特别的事情.

On the phone side, if you are extending WearableListenerService then this service is self managed by Android OS. Meaning, if the service were to receive a message or data from wear, Android would automatically create the service, service your request and destroy the service if required. You do not have to do any special thing here.

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

The intent defined in the manifest (copy pasted above) is good enough for Android OS to do the above stated management of the service.

这篇关于Android Wear:在手持设备上启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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