简体   繁体   English

当应用未运行时,从Android主机设备发送消息以穿戴

[英]Sending message from Android host device to wear when app is not running

I'm building a simple motivational quote app that generates a random quote from a MySQL database. 我正在构建一个简单的激励报价应用程序,该应用程序从MySQL数据库生成随机报价。 The app works fine on mobile and I want to sync the quote message to a Wear device. 该应用程序在移动设备上运行良好,我想将报价消息同步到Wear设备。 I'm using MessageApi to do so and used this tutorial to set it up: http://android-wear-docs.readthedocs.org/en/latest/sync.html . 我正在使用MessageApi进行操作,并使用了本教程进行设置: http : //android-wear-docs.readthedocs.org/en/latest/sync.html

However, the message functionality only works when the app is running on the host device. 但是,消息功能仅在应用程序在主机设备上运行时才起作用。 I need to launch the app on Wear alone and still be able to receive the message from the mobile device. 我需要单独在Wear上启动该应用,但仍然能够从移动设备接收消息。 I thought of running the same application on the Wear device where it will run an httpconnection on it's own but from what I understand this is not possible with Android Wear. 我曾考虑在Wear设备上运行相同的应用程序,该应用程序将自己运行httpconnection,但据我了解,Android Wear无法实现。

So my question is, is there a way to open an app on a Wear device that will trigger the mobile app to open in the background and hence receive the message that way? 所以我的问题是,是否有一种方法可以在Wear设备上打开应用程序,从而触发移动应用程序在后台打开并以这种方式接收消息?

Your mobile app can implement WearableListenerService. 您的移动应用程序可以实现WearableListenerService。 In that service implementation, you can listen to messages that are sent to your phone from your companion app on a wear device (when such message is sent to your phone, the framework on your phone starts up your service and passes the message to it). 在该服务实现中,您可以在穿戴式设备上侦听从伴侣应用程序发送到手机的消息(当此类消息发送到手机时,手机上的框架会启动您的服务并将消息传递给它) 。 You can then implement the logic you need in that service (fetch a quote, etc) and respond back to your wear's message; 然后,您可以在该服务中实现所需的逻辑(获取报价等)并回复您的着装信息; when its work is done, that service will go away on its own. 工作完成后,该服务将自行消失。 For this to work, your wear app needs to send such message to your phone app upon start up (or whenever you see appropriate in your app). 为此,您的穿戴应用需要在启动时(或在您认为合适的应用中)将此类消息发送到手机应用。 This approach should work and probably is the most appropriate approach for your use case. 这种方法应该有效,并且可能是针对您的用例的最合适的方法。

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

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