简体   繁体   English

带计时器的Asynctask。 即使应用程序不在后台运行,它也会运行吗?

[英]Asynctask with timer. Will it run even if the app is not running in the background?

I want to update my database on the server (using 000webhost) every 15 minutes, I searched and found aysnc task + timer will do what I need but I am not sure will it update my database even if the application is not running on the android device? 我想每15分钟更新一次服务器上的数据库(使用000webhost),我搜索发现aysnc任务+计时器可以满足我的需要,但是即使应用程序未在android上运行,我也不确定会更新我的数据库设备? Can anybody clear this doubt for me? 谁能为我消除这个疑问? This is what i will follow to do my task: Schedule AsyncTask to run every minute Also will this be the correct approach? 这是我要执行的任务:将AsyncTask安排为每分钟运行这也是正确的方法吗?

Yes, this approach will work even if your app is not running. 是的,即使您的应用未运行,此方法也可以使用。 But if your app is not running, why would you use an AsyncTask ? 但是,如果您的应用程序未运行,那么为什么要使用AsyncTask呢? The idea of the AsyncTask is that it provides handy methods to do some work in the background and easy post results on the UI thread afterwords. AsyncTask的想法是,它提供了方便的方法来在后台执行一些工作,并易于在UI线程后缀上发布结果。 If your app is not running you don't need the second part. 如果您的应用未运行,则不需要第二部分。

So a better solution for you would be to use the combination of Timer and IntentService . 因此,对您来说更好的解决方案是使用TimerIntentService的组合。 The former starts the latter which does the heavy-lifting (in your case update the database) and you're all done. 前者启动后者,后者进行繁重的工作(在您的情况下,更新数据库),您已经完成。

Lastly I would suggest you to take a look at the JobScheduler class. 最后,我建议您看一下JobScheduler类。 If it's not really critical to run your task every 15 minutes it's much better for the device's batter to use this one. 如果并不是每15分钟运行一次任务真的很关键,那么设备击球手最好使用此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM