禁用/启用连接时,不会在手持设备和可穿戴设备上触发 onCapabilityChanged [英] onCapabilityChanged is not fired on both handheld and wearable while disabling/enabling connections

查看:28
本文介绍了禁用/启用连接时,不会在手持设备和可穿戴设备上触发 onCapabilityChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<service android:name=".SMessage">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:host="*" android:pathPrefix="/send-message" android:scheme="wear" />
        </intent-filter>
    </service>


public class SMessage extends WearableListenerService {

@Override
        public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
        super.onCapabilityChanged(capabilityInfo);
        _A.toast("onCapabilityChanged");
        Set<Node> connectedNodes = capabilityInfo.getNodes();
        pickBestNodeId(connectedNodes);
    }
}

如果我启用飞行模式或禁用蓝牙连接,则不会触发此方法 onCapabilityChanged.

this method is not fired onCapabilityChanged if I enable aircraft mode or disable bluetooth connection.

我错过了什么?_A.toast 使用 Application 类显示 Toast.

What have I missed? _A.toast shows toast using Application class.

发送消息有效.

推荐答案

您需要包含一个 android:path 元素,用于标识您正在侦听的(完全限定的)功能:

You need to include an android:path element that identifies the (fully-qualified) capability you're listening for:

<intent-filter>
    <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
    <data android:host="*" android:scheme="wear" 
          android:path="/com.mypackagename.my_capability_name"/> 
</intent-filter>

其中 my_capability_name 是在其他设备上运行的应用的 wear.xml 中定义的:

where my_capability_name is defined in the wear.xml of your app running on the other device:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="android_wear_capabilities">
        <item>my_capability_name</item>
    </string-array>
</resources>

https://developer.android 所述.com/training/wearables/data-layer/messages.html#AdvertiseCapabilities.

但是有几点需要注意:

  • Because your app will run every time the devices dis/connect, you could see significant battery drain as a result.
  • As with all other manifest-declared broadcast receivers, this will stop working in Android O: https://developer.android.com/preview/features/background.html#broadcasts

这篇关于禁用/启用连接时,不会在手持设备和可穿戴设备上触发 onCapabilityChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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