android-discuss icon indicating copy to clipboard operation
android-discuss copied to clipboard

大家有没有做过后台定时任务的?都是怎么做的?比如我要每隔一分钟定位一次并上传位置到服务器。

Open lixiangers opened this issue 11 years ago • 12 comments

我用过AlarmManager,但是这种方式在小米手机上不行。小米room现在没短的间隔是5分钟。就算是你定义1分钟处理一次,它还是5分钟做一次。大家有没有好的解决方案分享?

lixiangers avatar Sep 11 '15 02:09 lixiangers

目前我是在小米手机上单独用一个后台服务跑一个线程来处理。但是这样做需要WakeLock,阻止手机休眠,会很耗电。

lixiangers avatar Sep 11 '15 02:09 lixiangers

service里面开启一个timer

ByteRax avatar Sep 11 '15 02:09 ByteRax

@tanranran 这样手机休眠了还会执行嘛,我没试过!

lixiangers avatar Sep 11 '15 02:09 lixiangers

休眠了还要上传吗,那样会比较耗电。要是硬要休眠了还上传,你可以试下设置保持CPU唤醒。

start141 avatar Sep 11 '15 02:09 start141

额,你自己已经说了这个方案了,别的办法我就不清楚了

start141 avatar Sep 11 '15 02:09 start141

@start141 这样有点O2O的APP,需要定时上传位置信息。不知道像滴滴打车之类的是怎么处理的。做是可以做,肯定是要高耗电的。

lixiangers avatar Sep 11 '15 02:09 lixiangers

滴滴是长链接?

start141 avatar Sep 11 '15 02:09 start141

长连接也没用,休眠后就会断开

2015-09-11 10:34 GMT+08:00 start141 [email protected]:

滴滴是长链接?

— Reply to this email directly or view it on GitHub https://github.com/android-cn/android-discuss/issues/252#issuecomment-139432736 .

spysoos avatar Sep 11 '15 02:09 spysoos

我也是service里设置timer

XepherJin avatar Sep 11 '15 03:09 XepherJin

试试SyncAdapter.

taoliuh avatar Sep 23 '15 02:09 taoliuh

可以尝试注册一个BroadCastReceiver,去接收系统发出的时间更新的广播。Intent.ACTION_TIME_TICK(每分钟发出一次)

erdanerdan avatar Oct 07 '15 07:10 erdanerdan

也可以用AlarmManager,比Timer更加精准

lizhenghlh avatar Oct 15 '15 08:10 lizhenghlh