CanvasWatchFaceService中的自定义广播接收器(Custom broadcast receiver in CanvasWatchFaceService)

编程入门 行业动态 更新时间:2024-10-20 01:32:37
CanvasWatchFaceService中的自定义广播接收器(Custom broadcast receiver in CanvasWatchFaceService)

我正在开发适用于Android Wear的一些表盘(minSdkVersion = 21和targetSdkVersion = 23)并集成适用于Android的Google Fit API。

我想在背景中调查具有给定用户选定时间间隔的适合数据,并将其传递回我的表盘。 所以我创建了一个服务,它扩展了Service类,实现了SensorEventListener,GoogleApiClientConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener等,我使用Fitness API来读取适合的数据和心脏传感器数据。

然后通过使用自定义BroadcastReceiver将值返回(卡路里,距离,步骤等)到监视面,这是一个扩展CanvasWatchFaceService的标准类。

我想要实现的是始终更新表盘中可用的健身变量,以便在用户与其交互时离开环境或静音模式时,我需要显示的数据已经存在,我只需要绘制它在屏幕上无需通过使用Fitness API读取它,因为它可能需要几秒钟的时间。

它的工作原理。 数据由广播接收机在钟表中接收,并且正确设置变量值。 我通过使用一个简单的canvas.drawText在屏幕上输出它们。 我看到他们更新交互模式(观看面部可见和活动),当然根据轮询时间间隔,甚至看到他们在环境模式更新时调用onTimeTick,如果我正在调试表盘(通过USB或蓝牙)。

只要我断开调试器并且手表表面处于环境模式,似乎消息停止被广播接收器接收。

我究竟做错了什么? 我真的很沮丧。

I'm developing some watch faces for Android Wear (minSdkVersion=21 and targetSdkVersion=23) and integrating the Google Fit API for Android.

I want to poll in the background for fit data with a given - user selected - time interval and pass it back to my watch face. So I created a service which extends Service class, implements SensorEventListener, GoogleApiClient ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener etc, and I use the Fitness API to read fit data and heart sensor data.

I then pass the values back (Calories, Distance, Steps etc.) to the watch face, which is a standard class extending CanvasWatchFaceService, by using a custom BroadcastReceiver.

What I want to achieve is to have always updated fitness variables available in the watch face, so that when it leaves ambient or muted mode because a user interacts with it, the data I need to show is already there and I just need to draw it on the screen without the need to read it by using the Fitness API at that moment, since it can take several seconds.

And it works. Data is received by the broadcast receiver in the watch face and variable values are correctly set. I output them on the screen by using a simple canvas.drawText. I see them update in interactive mode (watch face visible and active), of course according with the polling time interval, and even see them update in ambient mode when onTimeTick is called, if I'm debugging the watch face (either via usb or bluetooth).

As soon as i disconnect the debugger and the watch face goes in ambient mode, it seems that messages simply stop being received by the broadcast receiver.

What am i doing wrong? I'm really getting frustrated at this.

最满意答案

Android OS在完成处理后会立即终止后台Service ; 这是其生命周期的基础。 您可以在文档中阅读更多关于此的信息 。

对于您的用例,您可能需要设置BroadcastReceiver ,并使用AlarmManager.setExactAndAllowWhileIdle()以轮询间隔触发它。 将它转换为WatchFaceService子类中已有的BroadcastReceiver可能是有意义的,或者您可能需要创建一个新的来替换您的轮询Service 。

另外,我不知道你投票的频率如何,但在手表睡着的时候经常这样做可能会对电池寿命产生重大影响。 只是要记住的东西; 当手表已经睡了一段时间时,你可能想要做一些像频率逐渐减少的事情。

The Android OS will kill a background Service as soon as it finishes processing; that's basic to its lifecycle. You can read more about this in the documentation.

For your use case, you probably want to set up a BroadcastReceiver, and trigger it at your polling interval with AlarmManager.setExactAndAllowWhileIdle(). It might make sense to roll this into the existing BroadcastReceiver you already have in your WatchFaceService subclass, or you may want to create a new one to replace your polling Service.

Also, I don't know how frequently you're polling, but doing this too often while the watch is asleep is likely to have a significant impact on battery life. Just something to keep in mind; you may want to do something like a gradual decrease in frequency when the watch has been asleep for a while.

更多推荐

watch,face,API,电脑培训,计算机培训,IT培训"/> <meta name="description&qu

本文发布于:2023-08-06 21:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   自定义   CanvasWatchFaceService   receiver   broadcast

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!