android - Can't receive LocationClient's location updates after screen gone off -
i'm trying write simple android service runs in background , receives location updates locationclient (google map api android v2). problem when screen go off, service doesn't receives anymore location updates. tried check if service active, screen off, , (i have timertask scheduling log). when screen on can receive location updates, when screen goes off see timertask's logs , don't receive location update. waking screen turns on location updates again. how can solved?
here simple service:
public class locationservice extends service implements googleplayservicesclient.connectioncallbacks, googleplayservicesclient.onconnectionfailedlistener, locationlistener{ private static final string tag = locationservice.class.getsimplename(); private locationclient mlocationclient; private timer timer; private static final locationrequest request = locationrequest.create() .setinterval(5*1000) // 5 seconds .setfastestinterval(3*1000) // 3 seconds .setpriority(locationrequest.priority_high_accuracy); @override public void oncreate() { log.d(tag, "creating.."); mlocationclient = new locationclient(this, this, this); timer = new timer(); } @override public int onstartcommand(intent intent, int flags, int startid) { log.d(tag, "starting.."); if(!mlocationclient.isconnected() || !mlocationclient.isconnecting()) { log.d(tag, "connecting location client.."); mlocationclient.connect(); } timer.scheduleatfixedrate(new timertask(){ @override public void run() { log.d(tag, "timertask executing"); }}, 0, 3*1000); return start_sticky; } @override public void onconnectionfailed(connectionresult result) { log.d(tag, "connection failed.."); stopself(); } @override public void onconnected(bundle bundle) { system.out.println("connected ..."); mlocationclient.requestlocationupdates(request, this); } @override public void ondisconnected() { log.d(tag, "disconnected.."); stopself(); } @override public ibinder onbind(intent arg0) { throw new unsupportedoperationexception("not yet implemented"); } @override public void onlocationchanged(location location) { log.d(tag, "received location update"); } @override public void ondestroy() { log.d(tag, "destroying.."); timer.cancel(); mlocationclient.removelocationupdates(this); } }
use wakelock
in onstartcommand
wl = ((powermanager)getsystemservice(context.power_service)).newwakelock(powermanager.partial_wake_lock, yourtag); wl.acquire();
in ondestroy
if (wl != null && wl.isheld()) { wl.release(); }
Comments
Post a Comment