Android: LocationManager service stops updating when set to NETWORK_PROVIDER -
right, situation follows;
we have developed app checks location of user every 5-10 minutes location specific content. so, i've created service stick background (so can update when app isn't directly in foreground) in locationcontroller created check latest known location. when it's finished, cleans , location sent database (which necessity concept).
this works fine, long check location gps_provider. however, when switch around network_provider, may check location once more before dying completely.
i've tried multiple options prevent problem, nothing seems working update service when swap 1 setting.
here few snippets should relevant service:
updateservice:
@override public void oncreate() { super.oncreate(); log.d("debug", "created service"); powermanager mgr = (powermanager)getsystemservice(context.power_service); wakelock = mgr.newwakelock(powermanager.partial_wake_lock, "mywakelock"); wakelock.acquire(); intentfilter filter = new intentfilter(); filter.addaction(update_action); } @override public int onstartcommand(intent intent, int flags, int startid) { log.d("starting", "starting service"); mnotificationmanager = (notificationmanager) getsystemservice(notification_service); malarmmanager = (alarmmanager) this.getsystemservice(context.alarm_service); intent = new intent(update_action); alarmmanagerpendingintent = pendingintent.getbroadcast(this, 0, i, 0); alarmmanagerpendingintent.cancel(); malarmmanager.cancel(alarmmanagerpendingintent); malarmmanager.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup, alarmmanager.interval_fifteen_minutes, alarmmanager.interval_fifteen_minutes, alarmmanagerpendingintent); mlocationcontroller = new locationcontroller(this, this); updatehandler = new handler(); return start_sticky; }
locationcontroller:
private locationlistener locationlistener = new locationlistener() { private boolean didsendlocation; public void onlocationchanged(location location) { mlastlocation = location; mcallback.locationupdate(location,false); didsendlocation = true; } public void onstatuschanged(string provider, int status, bundle extras) { log.d("debug", "status changed: "+status); location _lastlocation = mlocationmanager.getlastknownlocation(locationmanager.network_provider); log.d("debug", "last location: "+_lastlocation); if(!didsendlocation) { mcallback.locationupdate(_lastlocation,false); } didsendlocation = false; } public void onproviderenabled(string provider) { log.d("debug", "provider enabled"); } public void onproviderdisabled(string provider) { log.d("debug", "provider disabled"); } }; public locationcontroller(context mcontext, locationcontrollercallback callback) { this.mcontext = mcontext; mcallback = callback; log.d("debug", "created locationcontroller"); mlocationmanager = (locationmanager) mcontext.getsystemservice(context.location_service); mlocationmanager.requestlocationupdates(locationmanager.gps_provider, 300000, 100, locationlistener); mlastlocation = mlocationmanager.getlastknownlocation(locationmanager.network_provider); mcallback.locationupdate(mlastlocation,true); }
so, code works now, when swap:
mlocationmanager.requestlocationupdates(locationmanager.gps_provider, 300000, 100, locationlistener);
with
mlocationmanager.requestlocationupdates(locationmanager.network_provider, 300000, 100, locationlistener);
it no longer update. ideas?
when use location updates:
mlocationmanager.requestlocationupdates(...)
you don't need alarmmanager
methods first parameter of requestlocationupdates
method acts "timer". see here more info.
also, may need include following directives in androidmanifest.xml file:
<uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.control_location_updates"/>
without these permissions, requestlocationupdates
method may not work expected regardless of provider use.
update 1:
i have locationcontroller
class initialiased in service, example:
@override public int onstartcommand(intent intent, int flags, int startid) { super.onstartcommand(intent, flags, startid); this.mlocationcontroller = new locationcontroller(this); return service.start_not_sticky; }
then, start above service in onresume
method of activity (mainactivity or activity in project), , pause service in onpause
method of same activity.
Comments
Post a Comment