每1分钟(60 * 1000毫秒)最多10小时调用一次api [英] Call api for every 1 minute(60*1000 milliseconds) upto 10 hours

查看:233
本文介绍了每1分钟(60 * 1000毫秒)最多10小时调用一次api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发在后台运行的应用程序,需要每1分钟(60 * 1000毫秒)调用api,且不会出现任何波动.我已经尝试了Scheduler,timer和其他东西,但是它不能正常工作.例如,我的情况是在09:11:36 am(第一个api调用),09:12:36 am(第二个api调用)上调用api,依此类推,最后,最终的api调用将在11:下午20:36.我用下面的代码:

I am working on the app that runs in background and need to call api for every 1 minute(60*1000 millisecond) without any fluctuations. I have tried Scheduler,timer and things but it is not working proper. For example, my scenario is to call the api on 09:11:36 am(first api call),09:12:36 am(second api call) and so on and at the end the final api call will be at 11:20:36 pm. I used below code :

Handler minuteHandler = new Handler();
minuteHandler.postDelayed(runnable, 60000);
final Runnable runnable = new Runnable() {
@Override
public void run() {
 // your runnable code
 minuteHandler.removeCallbacks(runnable);
 minuteHandler.postDelayed(runnable, 60000);
 }
};

new Timer("threadname", true).scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
   // your runnable code              
        }
    }, 0,60*1000);

等, 我的问题是,有可能在android(所有操作系统版本)中实现.

etc., My question is that it is possible to achieve in android(all OS vesions).

推荐答案

您可以在api级别26之前使用service来实现,但是在api级别27上,服务会被操作系统自动终止.

you can achieve that using service till api level 26 but in api level 27 service will automatically kill by the os.

这篇关于每1分钟(60 * 1000毫秒)最多10小时调用一次api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆