How to start a Service (that samples light data) o

2020-04-14 08:23发布

问题:

I am pretty new to Android Development and I am currently experimenting with wearable apps.

I created a background Service on my smartwatch that samples light sensor data and I want it to start at a certain time everyday. My current approach is to implement an alarm manager on my handheld device to trigger said service on the watch, but I'm not sure how to make this connection between the phone and watch.

From what I have researched I'm not sure if this is even possible. I guess my main questions are:

  1. Is it possible to use an alarm manager that is implemented on my phone to start a service on my watch? (I hope to make the start time user configurable in the future so ideally, I hope this can be done on the phone!)
  2. If so, how can this be done? I have an inkling a the messageApi or broadcast receiver can be used, though I'm not sure...?
  3. If not, would it be inefficient or considered bad design to have the alarms implemented in the wearable app instead?

Any help would be greatly appreciated!

回答1:

1.Is it possible to use an alarm manager that is implemented on my phone to start a service on my watch?

Haven't tried it but try checking the Building Apps for Wearables documentation it will teach you how to build notifications in a handheld app that are automatically synced to wearables as well as how to build apps that run on wearables.


2.If so, how can this be done? I have an inkling a the messageApi or broadcast receiver can be used, though I'm not sure...?

Sending and Receiving Messages:

You send messages using the MessageApi and attach the following items to the message:

  • An arbitrary payload (optional)
  • A path that uniquely identifies the message's action

3.If not, would it be inefficient or considered bad design to have the alarms implemented in the wearable app instead?

Scheduling Repeating Alarms:

Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.

NOTE: Try this tutorial : Android Wear Development