我是否需要为使用AlarmManager并激活锁定定期使用LocationClient? [英] Do I need to Use an AlarmManager and WakeLock to periodically use a LocationClient?
问题描述
从一年前的帖子表明,它是需要使用激活锁定保留CPU收集位置信息(CommonsWare尤其是这里的Android定期GPS位置更新与AlarmManager服务内)。不过,我不明白为什么我不能简单地调用一个服务,每28分钟,连接LocationClient,设置定时器等待2分钟,然后拿到最近的位置,断开客户端,然后停止该服务。
由于Commonsware,谁在这里分叉他的榜样用户(<一href=\"https://github.com/alexbirkett/cwac-locpoll/blob/master/src/com/commonsware/cwac/locpoll/LocationPollerService.java\" rel=\"nofollow\">https://github.com/alexbirkett/cwac-locpoll/blob/master/src/com/commonsware/cwac/locpoll/LocationPollerService.java)用LocationListener的,而不是一个LocationClient,我想知道他的答案是否还aplies。
感谢您的时间!
编辑:难道这个简单,其中一个AlarmManager调用一个服务,每28分钟数>服务获得一个激活锁定和两分钟的计时器经过的LocationClient是连接>超过2分钟已经过去,我可以杀死服务并释放激活锁定?
这将是这个样子:
电源管理PM =(电源管理)getBaseContext()getSystemService(Context.POWER_SERVICE)。
PowerManager.WakeLock WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,);
wl.acquire(); 定时器theTimer =新的Timer();
theTimer.schedule(新的TimerTask(){ @覆盖
公共无效的run(){
如果(checkIfGooglePlay()){
getPostLocation();
w1.release();
stopSelf();
}
}
},TWO_MINUTES); 返回Service.START_NOT_STICKY;
不过,我不明白为什么我不能简单地调用一个服务,每28分钟,连接LocationClient,设置定时器等待2分钟,然后拿到最近的位置,断开客户端,然后停止该服务。
块引用>您也许可以轮询
LocationClient
。不过,你仍然需要一个激活锁定
,因为该设备将这两分钟内入睡,否则,您仍然需要AlarmManager
为每28分钟的一部分,我不知道是否LocationClient
将实施超出其他要求。Posts from over a year ago suggest that it is necessary to use a WakeLock to reserve CPU to gather location information (CommonsWare in particular here Android Periodic GPS location updates with AlarmManager inside a Service). However I do not understand why I cannot simply call a Service every 28 minutes, connect the LocationClient, set a timer to wait 2 minutes, then get the most recent location, disconnect the client, and then stop the service.
Because Commonsware and the user who forked his example here (https://github.com/alexbirkett/cwac-locpoll/blob/master/src/com/commonsware/cwac/locpoll/LocationPollerService.java) use a LocationListener, and not a LocationClient, I was wondering whether his answer still aplies.
Thanks for your time!
EDIT: Is it this simple, where an AlarmManager calls a Service every 28 minutes > the Service acquires a WakeLock and a timer elapses for two minutes as the LocationClient is connect > once two minutes has elapsed, I can kill the service and release the WakeLock?
It would look something like this:
PowerManager pm = (PowerManager) getBaseContext().getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); wl.acquire(); Timer theTimer = new Timer(); theTimer.schedule(new TimerTask() { @Override public void run() { if(checkIfGooglePlay()) { getPostLocation(); w1.release(); stopSelf(); } } }, TWO_MINUTES); return Service.START_NOT_STICKY;
解决方案However I do not understand why I cannot simply call a Service every 28 minutes, connect the LocationClient, set a timer to wait 2 minutes, then get the most recent location, disconnect the client, and then stop the service.
You probably can poll the
LocationClient
. However, you will still need aWakeLock
, as the device will fall asleep during those two minutes otherwise, you will still needAlarmManager
for the "every 28 minutes" part, and I don't know ifLocationClient
will impose other requirements beyond those.这篇关于我是否需要为使用AlarmManager并激活锁定定期使用LocationClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!