Android Wear:在Handheld上启动服务

前端之家收集整理的这篇文章主要介绍了Android Wear:在Handheld上启动服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个Wear应用程序,它将与掌上电脑上的WearableListenerService进行通信.
但是,我想确保当应用程序在手表上启动时服务已启动并运行.

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

在手表方面:

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

在手持设备方面:

  1. public class WearableBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context,Intent intent) {
  4. Intent startServiceIntent = new Intent(context,WearableService.class);
  5. context.startService(startServiceIntent);
  6. }
  7. }
  8.  
  9. <receiver android:name=".service.wearable.WearableBroadcastReceiver" >
  10. <intent-filter>
  11. <action android:name="my.wearable.CONNECT" />
  12. </intent-filter>
  13. </receiver>

解决方法

有几件事需要澄清

> Android Wear设备和Android设备之间的通信接口如下

> DataApi
> MessageApi

使用NodeApi获取连接的节点ID.
>您不能从磨损方发送意图并期望在手机端接收它.
>在电话方面,如果您要扩展WearableListenerService,则此服务由Android OS自行管理.这意味着,如果服务是从磨损中接收消息或数据,Android将自动创建服务,为您的请求提供服务并在需要时销毁服务.你不必在这做任何特别的事情.

清单中定义的意图(上面粘贴的副本)足以让Android OS完成上述服务管理.

猜你在找的Android相关文章