java - trying to get location from "network" provider fails on some devices despite the provider being enabled -
i'm trying write android app gets current location 'network' provider (so should need android.permission.access_coarse_location
permission). works fine on devices, on others don't seem able location update. here's code.
in oncreate:
locationmanager = (locationmanager) getsystemservice(context.location_service); provider = locationmanager.network_provider; log.v (tag, "finding location provider: " + provider); if (check_provider_enabled && !locationmanager.isproviderenabled (provider)) { toast.maketext (this, "location provider disabled", toast.length_long).show (); } else { try { location location = locationmanager.getlastknownlocation(provider); if (location == null) { log.i (tag, "getlastknownlocation returned null, requesting updates when available"); locationmanager.requestlocationupdates (provider, 0, 0, locationlistener); } else locationlistener.onlocationchanged (location); } catch (illegalargumentexception e) { log.e(tag, "failed location provider " + provider, e); toast.maketext (this, "location provider disabled", toast.length_long).show (); } }
locationlistener defined follows:
private locationlistener locationlistener = new locationlistener() { @override public void onstatuschanged (string provider, int status, bundle extras) { } @override public void onproviderenabled (string provider) { } @override public void onproviderdisabled (string provider) { } @override public void onlocationchanged (location location) { toast.maketext (carouselvieweractivity.this, "" + location.getlatitude () + ", " + location.getlongitude (), toast.length_long).show (); log.v(tag, "" + location.getlatitude () + ", " + location.getlongitude ()); locationmanager.removeupdates (this); } };
on phones work, here works fine: if check_provider_enabled true, isproviderenabled
test returns true, getlastknownlocation
returns location rather null, , if force request updates receive 1 details of location.
but on other phones (the specific 1 i'm testing galaxy s2 cyanogenmod 9) isproviderenabled
test returns false (despite settings item enabling network location being turned on - see locationmanager.isproviderenabled(locationmanager.network_provider) not reliable, why? summary of issue), getlastknownlocation
returns null, , after requesting updates not receive 1 in reasonable time (i've waited 5 minutes).
is there wrong approach i'm taking? there other way of getting network location need, or setting on phone other enabling network location that's required work? @ more basic level, going wrong here?
similar issues relating android network provider have been reported before on stack overflow can find here. consensus have experienced problem state there 2 possible workarounds, rebooting device when issue becomes apparent, or utilizing google play service api retrieving location data there appears issue reliability of location manager android 4.0 or newer.
Comments
Post a Comment