geolocation - Android LocationUpdates not working -
i trying implement location update listener detect loction change in android.i have implemented following code. notification log "gps started". after onlocationchanged() function never calls. tested changing location never called. note gps enabbled.
public class locationactivity extends activity implements locationlistener{ locationmanager lmanager=null; textview _textview; void initlocationservice() { { lmanager=(locationmanager)getsystemservice(context.location_service); criteria criteria=new criteria(); criteria.setaccuracy(criteria.accuracy_fine); gpsstatus.listener gpslistener=new gpsstatus.listener() { @override public void ongpsstatuschanged(int event) { if(event==gpsstatus.gps_event_stopped) { log.e("gps listener","gps stoped"); } else if(event==gpsstatus.gps_event_started) { log.e("gps listener","gps started"); } } }; locationrequest lrequest=new locationrequest(); lmanager.addgpsstatuslistener(gpslistener); } } @override public void onlocationchanged(location location) { // todo auto-generated method stub log.e("gps listener","locationchanged"); _textview.settext("locationchanged"+count); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub log.e("gps listener","statuschanged"); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub log.e("gps listener","providedenabled"); } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub log.e("gps listener","providerdisabled"); } enter code here @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.location_test); _textview=(textview)findviewbyid(r.id.textview1); initlocationservice(); } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); } @override protected void onpause() { // todo auto-generated method stub super.onpause(); lmanager.removeupdates(this); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); lmanager.requestlocationupdates(locationmanager.gps_provider, 400, 0,this); }
}
to location updates, need call requestlocationupdates()
, give locationlistener
.
typically, you'd define locationmanager , locationlistener like:
locationmanager locmgr = null; locationlistener loclistener = null; loclistener = new locationlistener() { public void onlocationchanged(location location) { if (location != null) { // } } public void onproviderdisabled(string provider) { } public void onproviderenabled(string provider) { } public void onstatuschanged(string provider, int status, bundle extras) { } };
and enable in onresume()
call like:
locmgr.requestlocationupdates(locationmanager.gps_provider, 0, // min time in ms 0, // min distance in meters loclistener);
and in onpause()
:
locmgr.removeupdates(loclistener);
Comments
Post a Comment