在安卓5.1系统中安排每一秒的报警时间[英] scheduling alarm for every second in android 5.1

本文是小编为大家收集整理的关于在安卓5.1系统中安排每一秒的报警时间的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述


我想在我的应用程序中每秒运行报警服务.它在5.1版下工作正常.但它在5.1设备中未触发.我正在使用Communesware醒来的意图服务.Logcat消息令人疑问地说,"可疑的短暂间隔1000毫升;扩展到60秒".我如何在5.1中调用每一秒钟?任何人都可以建议我如何实现这一目标?

解释一点:

我的用例是我需要每30分钟的时间内执行一些操作.使用警报管理器的AFAIK是有效的方式,但这里

1)我需要向用户显示计数计时器. (定时器任务,计数计时器,scheduledexecutorservice对此非常有用)
2)我需要向用户通知每30分钟(通过通知)即使应用程序在后台.(警报服务足够)

但在这里我的问题是当应用程序在后台时,当您从rucess(即,应用程序进程遇到的应用程序中的应用程序时,没有服务或定时器,处理程序,执行程序服务将无法正常工作).在这种情况下,我如何在完成30分钟后通知用户.如果我以错误的方式思考,请指导我.

谢谢,
Chaitanya

推荐答案

这是Android棒棒糖中的正常行为.

可疑的短暂间隔1000毫升;扩展到60秒

告诉你,系统不再喜欢这些短时间间隔.

问题#161244 记录在其中:

这是按预期工作的,但目前尚未记录(我们知道问题的那一方).

非常一般地说:短时间内和近期警报在电池中昂贵昂贵;需要短期或近期工作的应用程序应该使用其他机制来安排他们的活动.

所以不要为此使用AlarmService.喜欢线程或Executors或TimerTask或其他东西:

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

// Using Executors
Executors.newSingleThreadScheduledExecutor().schedule(runnable, 1, TimeUnit.SECONDS);

其他推荐答案

为什么你会这样做?
使用处理程序:

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

并使用以下用于停止您的1秒定时器:

mHandler.removeCallbacks(runnable);

其他推荐答案

使用1 和 2:

  • 使用AlarmManager以便以大于一分钟的间隔提醒用户的作用(例如请求的30分钟)

  • 如果通知触发了一个活动,您需要显示更新的活动是在前景中,然后也做一些便宜的,如postDelayed(),给予该活动中的用户定期更新

本文地址:https://www.itbaoku.cn/post/102515.html

问题描述


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

推荐答案

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

其他推荐答案

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

其他推荐答案

Use both 1 and 2:

  • Use the AlarmManager for the role of alerting the user at an interval greater than one minute (such as the requested 30 minutes)

  • If the notification triggers an activity where you need to show updates is in the foreground, then also do something cheap, like postDelayed(), to give the user periodic updates in that activity