对于每一秒在安卓5.1的调度报警每一秒

2023-09-06 06:05:41 作者:感情早已陌生

我要为每一秒在我application.It运行报警服务工作正常,低于5.1的版本。但它不触发5.1设备。我使用commonsware清醒意图的服务。logcat的消息说,觉得蹊跷短的时间间隔1000米利斯;扩大到60秒。我怎样才能轮询每秒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?

说明一点的:

我用例是我需要做一些操作,每间隔30分钟。 AFAIK对于这种使用警报管理器是有效的方法,但在这里

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)我需要显示的倒数计时器给用户。 (定时任务,倒计时,ScheduledExecutorService的是pretty的这个很有用) 2)我需要通知的每个30分钟用户(经由通知),即使该应用是在背景。(报警服务是不够此)

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)

但在这里,我的问题是,当应用程序在后台,当你刷卡从最近通话(即应用程序进程被杀死),没有任何服务或定时器,处理程序,执行程序服务将无法正常工作)的应用程序。在这种情况下,我怎么能通知30分钟结束后的用户。请指导我,如果我错了的方式在想。

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

推荐答案

这是在Android的棒棒堂正常的行为。

This is normal behavior in Android Lollipop.

蹊跷短的时间间隔1000米利斯;扩大到60秒

Suspiciously short interval 1000 millis; expanding to 60 seconds

告诉你的系统不喜欢那些很短的时间间隔了。

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

发行#161244 记载,:

这是如预期,虽然是在present充分证明(而且我们已经意识到这个问题的那一面)。

This is working as intended, though is at present inadequately documented (and we're aware of that side of the problem).

概而言之:短周期和不久将来报警电池惊人地昂贵;需要短周期或近未来工作的应用程序应该使用其他机制来安排他们的活动。

所以,不要使用 AlarmService 这一点。 preFER线程或执行人的TimerTask 或别的东西:

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