Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scheduling alarm for every second in android 5.1


I want to run alarm service for every second in my application.It is working fine below 5.1 version. but it is not triggering in 5.1 devices. I am using commonsware wakeful intent service.The logcat message is saying that "Suspiciously short interval 1000 millis; expanding to 60 seconds". How can I poll for every second in 5.1? Can anybody suggest me how to achieve this?

Explanation little bit more:

My use case is I need to do some operation for every 30 minutes interval. AFAIK For this Using alarm manager is efficient way, but here

1)I need to display the count down timer to the user. (Timer task,Count down timer,ScheduledExecutorService is pretty useful for this)
2) I need to notify the user for every 30minutes(via notification) even if the app is in background.(Alarm Service is enough for this)

but here my problem is when the app is in background,when you swipe out the application from recents( i.e.,application process is killed) none of the services or timers,handlers,executor services will not work). In this case how can I notify the user after completion of 30 minutes. Please guide me if I am thinking in wrong way.

Thanks,
Chaitanya

like image 375
Chaitu Avatar asked Apr 28 '15 08:04

Chaitu


2 Answers

This is normal behavior in Android Lollipop.

Suspiciously short interval 1000 millis; expanding to 60 seconds

Tells you that the system does not like those short time intervals anymore.

Issue #161244 documented that:

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 don't use an AlarmService for this. Prefer a thread or Executors or TimerTask or something else:

// Using Handler
new Handler().postDelayed(runnable, TimeUnit.SECONDS.toMillis(1));

// Using Executors
Executors.newSingleThreadScheduledExecutor().schedule(runnable, 1, TimeUnit.SECONDS);
like image 138
shkschneider Avatar answered Oct 21 '22 14:10

shkschneider


Why would you do that?
Use an handler instead:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // do your stuff here, called every second
        mHandler.postDelayed(this, 1000);
    }
};

// start it with:
mHandler.post(runnable);

And use the following to stop your 1 sec timer:

mHandler.removeCallbacks(runnable);
like image 22
Lazy Ninja Avatar answered Oct 21 '22 13:10

Lazy Ninja