android - onLocationChanged not called on some devices -
this question has been asked numerous times not find solution works.
i developing application using fused location provider. in onconnected()
method, requesting location updates , application logic initiated once location fix generated , onlocationchanged()
called. (please refer code below).
problem onlocationchanged()
method never called on devices. use samsung tab 2 , samsung galaxy grand testing. code works fine on tab 2 not work on grand. not work, mean locationclient
gets connected onlocationchanged()
never called.
earlier, used location manager getting location , in implementation, same problem occurred. so, tried implementing fused location provider still same problem.
can me out issue? there missing here?
public class mainactivity extends activity implements googleplayservicesclient.connectioncallbacks, onconnectionfailedlistener, locationlistener { locationclient locationclient; locationrequest lr; location loc1; static string address; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); locationclient = new locationclient(this,this,this); locationclient.connect(); } @override public void onconnected(bundle arg0) { // todo auto-generated method stub lr=locationrequest.create(); lr.setinterval(100); locationclient.requestlocationupdates(lr, this); log.d("locationclient","on connected"); } @override public void ondisconnected() { // todo auto-generated method stub locationclient.disconnect(); } @override public void onconnectionfailed(connectionresult arg0) { // todo auto-generated method stub } @override public void onlocationchanged(location loc) { // todo auto-generated method stub // application logic log.d("locationclient","last known location lc:" + loc.getlatitude() + "," + loc.getlongitude()); } }
i observed same behavior on galaxy nexus , recognized 2 things causing onlocationchanged()
not being called. there are
corresponding location source not enabled in system settings.
priority_high_accuracy
gps satellites must enabled,priority_balanced_power_accuracy
wi-fi & mobile network location must enabled.even enabled location sources there can no valid location information available. instance,
priority_high_accuracy
source enabled, there no (or not good) gps reception (e.g. device inside building). orpriority_balanced_power_accuracy
source enabled, both wi-fi , mobile data switched off. in cases location cannot detected ,onlocationchanged()
not called.
to fix #1 had check whether required location sources enabled before connecting google services api , if switched off, notified user asked him/her allow location access.
to solve #2 decided use locationrequest.setexpirationduration()
. set timeout @ 10 seconds, , after calling requestlocationupdates()
post callback delayed same 10 seconds. if delayed callback called before requestlocationupdates()
, means location cannot detected due reason #2. 3 retries , show user error message.
android documentation not great @ place, that's why hope helps.
Comments
Post a Comment