NETWORK_PROVIDER not providing updated locations
Asked Answered
M

5

48
LocationManager locationManager = (LocationManager)getApp().getSystemService(Context.LOCATION_SERVICE); //getApp() returns my Application object
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1, 1, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);

That's the code I'm using to listen. With GPS enabled, everything works fine. However, if I disable GPS and rely on network location, it gives me stale results -- in this case, from two days ago. I cannot get it to update. Calling getLastKnownLocation returns the same stale results.

Google Maps updates just fine, so I know it's not a hardware/system configuration problem.

I've Googled around, and found a few people with similar problems, but no answers. I've tried targeting my project to API level 8 (2.2) as well as 15 (4.0.3). Same results. My phone is running ICS.

I've also tried removing the request for GPS_PROVIDER, as well as including requests for everything in getAllProviders(). Same deal.

Any idea why NETWORK_PROVIDER is not updating? Any help would be greatly appreciated.

(P.S. No, I don't I normally use "1, 1" as my time/distance parameters; I just changed it while debugging this.)

EDIT: I forgot to mention that yes, isProviderEnabled() returns true.

Maxia answered 27/11, 2012 at 23:13 Comment(12)
I've encountered this exact problem too, on S3 with Android 4.1.1: Google Maps continues to work with NETWORK_PROVIDER, but all other apps (including mine) don't get any updates. Uninstalling and reinstalling the app does not help. Only fix is to reboot the phone, after which things work as expected.Unsound
Same Problem: #15748043 if you discover something new, let me know pleaseWorldwide
Just for more info I'm also seeing this behaviour on my Nexus S. Going to try to reboot now and see if that fixes it.Delocalize
Running adb reboot seems to have fixed the issue for now. I'll update if it stops working soon.Delocalize
It just stopped working again randomly, I can't think of any actions that I take beforehand to make it not work. I know it isn't the code since I'm also running the same app on an S2 and it hasn't experienced this problem. My Nexus S is running 4.1.1 as is the S2 so not even sure it's an OS issue.Delocalize
Same problem here. On 4.1 and 4.0. My app just doesn't receives anything anymore, and after a reboot this is solved. However, I need this wifi stuff, and I can't ask users to reboot. :(Thanhthank
Just as a check: Is everybody unregistering their listeners on activity close or app exit?Thanhthank
Same problem on S3 Mini Android 4.1.2 SDK 16. As was mentioned before - only reboot resolves the problem. I have also encountered that this is caused in most cases by battery running under 10%... Obviously all apps (except Google maps) stop getting location updates (e.g. Facebook is still showing the same location). Is there any advancement on this problem?Certification
Also having this issue on my Galaxy S4. I am only using NETWORK_PROVIDER in my code. I can tell that my app is using a location on the way back from an out of town trip. Perhaps the provider's behavior changes as the battery gets low but then doesn't recover after charging? I'd really like to avoid using Play since one of my requirements is to support the Kindle.Secondrate
linking related questions #13595432 #15748043 #17735404 #16014101 #17169643Pathy
its a bug in android CHECK THIS OUTGaw
Unfortunately, this problem still persist. I'm compiling my code in SDK version 24, and running on Android 5.1.1Rusert
C
23

I finally found a way to solve this problem (see my question here).

In my case on S3 Galaxy Mini there were similar symptoms (no location updates until reboot...) like the ones described above. Location in most cases stopped updating when the device reached low power conditions, but sometimes it just happened even when the phone was fully charged. Obviously this is a problem somewhere in the LocationManager. I managed to bypass LocationManager by using the new Google Play Services API for locations (LocationClient class).

This is probably the reason why the location updates were still working with Google Maps, because Google Maps were using Google Play Services API even before it was made public.

I recommend you to follow this link which shows the way how to use Play Services API to get locations instead of using LocationManager if you encounter this problem.

Im running location updates with Play Services and there were absolutely no problems like this anymore - I keep receiving regular location updates anytime.

Beware that this will require that the user must have Google Play installed on their device, so Kindles and the like will be out of the picture. It also requires a minimum of Android 2.2.

Certification answered 30/7, 2013 at 21:25 Comment(4)
I'll try adding this to my app over the next few weeks and see how things go, hope it works!Delocalize
This is absolutely the solution. I struggled with this problem for days, and this fixed it!! Thank you!Hemianopsia
Yeah, supporting Kindle is one of my requirements. Out of curiosity though, how did this go?Secondrate
Also, just to confirm, according to this it does appear that Google is pushing the Play API. Since I need to support Kindle this poses a problem.Secondrate
T
0

I currently suspect it is caused by not using removeUpdates(listener) on app shutdown or activity ondestroy. I have made sure to implement that and hope it fixes it. I can't think of anything else.

--edit-- This did not fix it unfortunately.

Thanhthank answered 15/5, 2013 at 17:51 Comment(3)
Hey, did you notice any improvement??Rheinlander
Nope, unfortunately the app did suffer sometimes from the bug. So this is not a fix.Thanhthank
Thanks, wouldn't think so either, since i'm pretty sure i do stuff by the book, and i still get it... :(Rheinlander
E
0

If you are working on new phone you have to check if you have accepted all agreements in google maps app - else you will not receive any updates from NETWORK_PROVIDER.

Elvaelvah answered 9/8, 2013 at 12:27 Comment(0)
H
-2

1) first you check whether you have give the permission that is suggested above. 2) you must sure that internet is working in your handset. 3) please find the below code.

public void getLocationByNetworkProvider()
{       
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, new LocationListener() {
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub


        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        public void onLocationChanged(Location location) {
         location.getLatitude();                                location.getLongitude();

        }
    });     

}`
Hosey answered 27/7, 2013 at 14:10 Comment(2)
Maybe bacause of the many typos and the repeated useless information?Iatrochemistry
Hi, if isProviderEnabled() returns true then please check which one provider is on , this may either GPS provider or may be network provider.that's way other applications shows updated locations. when you call method locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER , 1, 1, this); then you will receive updated location in the method onLocationChanged(Location location) of your location listener..Hosey
S
-3

you can use like this for update location.Locationlistener will work for location....

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener{

protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public void onLocationChanged(Location location) {

txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" +      location.getLongitude());

}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}


}
Serendipity answered 16/8, 2013 at 10:30 Comment(2)
This is basically the way it should usually work, its the basic code for requesting locations. Im sure this is what the OP already knows how to do.Certification
hey @Teraiya Mayur, simply you changed the provider!!! this not proper answer bro.Evanesce

© 2022 - 2024 — McMap. All rights reserved.