0

In my app, I have to make it vibrate in time intervals defined by the user.

Basically it has a chronometer that makes the device vibrate when it has reached the defined time. Currently I do that by reading the chronometer value and comparing it with the desired value, in the onChronometerTick() method

I need this app to work in the background too. I've googled and found some examples about the android Service class.

My doubt is how my Service class will get the chronometer value? I've seen that the Service class can't handle layout components.

How can I do this? Is service the best way to handle this issue?

Thank you!

gattsbr
  • 3,762
  • 1
  • 22
  • 34
androidevil
  • 9,011
  • 14
  • 41
  • 79

1 Answers1

2

It sounds like what you want is an alarmmanager. Here's what it says regarding alarm managers:

These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

So if you're going to want the counter to continue after turning it off, you'll have to find an on-boot option or something. But yes, this is what you need to schedule events if you want them to keep happening after the screen locks.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
rofls
  • 4,993
  • 3
  • 27
  • 37
  • 1
    Thank you! But my currently app continues counting in backgound. I know this because when I minimize the app and return to it, the chronometer is counting correctly. The problem is tha it stops vibrating – androidevil Dec 15 '12 at 20:55
  • Did you find a solution for this? I am facing the same issue – abhishek maharajpet May 31 '20 at 15:10