scheduling alarm for every second in android 5.1

后端 未结 7 2202
醉梦人生
醉梦人生 2020-12-05 20:04


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 wa

7条回答
  •  伪装坚强ぢ
    2020-12-05 20:40

    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);
    

提交回复
热议问题