简体   繁体   English

WearableListenerService onCreate 和 onDataChanged 没有被调用

[英]WearableListenerService onCreate and onDataChanged not getting called

I'm trying to send data using the DataClient from a phone to a watch.我正在尝试使用 DataClient 从手机向手表发送数据。

Things I looked out for:我注意的事情:

  • same package name相同的 package 名称
  • no build flavors on both modules两个模块都没有构建风格
  • added service to the wear modules manifest向磨损模块清单添加服务
  • same path prefix相同的路径前缀
  • same signing config相同的签名配置

I tried this sample project and copied parts over to my project.我尝试 了这个示例项目并将部分复制到我的项目中。 I just can't find any issues with it.我只是找不到任何问题。

The sample project ran fine on my hardware, interestingly enough it wasn't working in the emulator.示例项目在我的硬件上运行良好,有趣的是它在模拟器中无法运行。 Therefore I tested my app also only with my hardware.因此,我也只用我的硬件测试了我的应用程序。 (Pixel 6 Pro & Pixel Watch) (Pixel 6 Pro 和 Pixel 手表)

The sending data part seems to be working, as it behaves the same way as the sample project does.发送数据部分似乎在工作,因为它的行为方式与示例项目相同。

How I send data from the phone:我如何从手机发送数据:

class WearDataManager(val context: Context) {

private val dataClient by lazy { Wearable.getDataClient(context) }

companion object {
    private const val CLIENTS_PATH = "/clients"
    private const val CLIENT_LIST_KEY = "clientlist"
}

fun sendClientList(clientList: MutableList<String>) {
    GlobalScope.launch {
        try {
            val request = PutDataMapRequest.create(CLIENTS_PATH).apply {
                dataMap.putStringArray(CLIENT_LIST_KEY, arrayOf("clientList, test"))
            }
                .asPutDataRequest()
                .setUrgent()

            val result = dataClient.putDataItem(request).await()

            Log.d("TAG", "DataItem saved: $result")
        } catch (cancellationException: CancellationException) {
            throw cancellationException
        } catch (exception: Exception) {
            Log.d("TAG", "Saving DataItem failed: $exception")
        }
    }
}
}

This is how I'm receiving data on the watch:这就是我在手表上接收数据的方式:

class WearableListenerService: WearableListenerService() {

companion object {
    const val CLIENTS_PATH = "/clients"
}

override fun onCreate() {
    super.onCreate()
    Log.d("testing", "STARTED SERVICE")
}

override fun onDataChanged(dataEvents: DataEventBuffer) {
    super.onDataChanged(dataEvents)

    Log.d("testing", "RECEIVED $dataEvents")
   
}
}

Surprisingly "STARTED SERVICE" does not appear in the log when I start the app on the watch.令人惊讶的是,当我在手表上启动应用程序时,“STARTED SERVICE”并没有出现在日志中。 For my understanding that means that the system isn't aware of the listeners existance and didn't register it.据我了解,这意味着系统不知道听众的存在,也没有注册。 So something must be wrong with the manifest below.所以下面的清单一定有问题。

This is the service inside the manifest on the watch:这是手表清单中的服务:

<service android:name=".wear.communication.WearableListenerService"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
        <data
            android:host="*"
            android:pathPrefix="/clients"
            android:scheme="wear" />
    </intent-filter>
</service>

What am I missing here?我在这里错过了什么?

Turns out the sending part was the culprit after all.原来发送部分毕竟是罪魁祸首。 Be careful what scope you use or if you even want to use one at all.小心你使用的是什么 scope 或者你是否想使用一个。 This function is being called inside of a worker in my code so it isn't an issue.这个 function 是在我的代码中的一个工作人员内部调用的,所以这不是问题。

I completely modified the demo project above and with the help of this I found out why it wasn't working.我完全修改了上面的演示项目,并借助它找到了它不起作用的原因。

This is the working solution:这是工作解决方案:

 fun sendClientList(clientList: MutableList<String>) {
    val request = PutDataMapRequest.create(CLIENTS_PATH).apply {
        dataMap.putStringArray(CLIENT_LIST_KEY, arrayOf(clientList.joinToString()))
    }
        .asPutDataRequest()
        .setUrgent()

    val result = dataClient.putDataItem(request)
    Log.d("TAG", "DataItem saved: $result")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM