-2

Hello Guys

I want to create an alarm which run every second . I have searched many code but fond no solution , Please suggest some references .

Thanks Amit Sharma

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Amit Sharma
  • 926
  • 2
  • 10
  • 35
  • 2
    Possible duplicate of [Repeating Alarm in Android](http://stackoverflow.com/questions/32001783/repeating-alarm-in-android) – BSMP Nov 04 '15 at 14:18

2 Answers2

0

You can do this, create a CountDownTimer , say how long you want it to last for in the first param (in milliseconds), then set a period of time to run a piece of code in the second param. In the onTick() method, this is the code that will run in the interval specified in the second param, onFinish() is called when the period of the countdown is finished. Call the start() method on the CountDownTimer object when you want it to run.

int howLongTimerLastsInMilliseconds =  3000 // 3000 milliseconds 
int tickEverySecond = 1000 // 1000 milliseconds == 1 second

CountDownTimer countDownTimer = new CountDownTimer(howLongTimerLastsInMilliseconds,tickEverySecond  ) {

                    @Override
            public void onTick(long millisUntilFinished) {
             //do some work every second  

            }

            @Override
            public void onFinish() {
              //do something when the time is up 
                }
        };

        countDownTimer.start();
Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
0

According to the documentation:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

Also this question was rised at code.googls.com and here is an explanation of it:

Suspiciously short interval 5000 millis; expanding to 60 seconds

This is working as intended, though is at present inadequately documented (and we're aware of that side of the problem). Speaking very generally: short-period and near-future alarms are startlingly costly in battery; apps that require short-period or near-future work should use other mechanisms to schedule their activity.

So, there is no inbuild way how to solve your issue using Alarm. And you should look out for another mechanisms or (very rough solution) use 60 independent alarms

DmitryArc
  • 4,757
  • 2
  • 37
  • 42