onLocationChanged not called on some devices
Asked Answered
W

7

20

This is a question which has been asked numerous times but I could not find a solution that always works.

I am developing an application using the Fused location provider. In the onConnected() method, I am requesting for location updates and the application logic will be initiated once a location fix is generated and onLocationChanged() is called. (Please refer to my code below).

Problem onLocationChanged() method is never called on some devices. I use a Samsung Tab 2 and a Samsung Galaxy Grand for testing. This code works perfectly fine on the Tab 2 but does not work on Grand. By does not work, I mean that locationClient gets connected but onLocationChanged() is never called.

Earlier, I used the location manager for getting location and in that implementation, the same problem occurred. So, I tried implementing the fused location provider but I still get the same problem.

Can anyone help me out with this issue? Is there something I am 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());
}
}
Wivern answered 7/8, 2013 at 17:8 Comment(5)
If onConnected() will not be called onConnectionFailed() should be fired. You had a look on the ConnectionResult?Jerk
onConnected() is being called and connection is established. I have checked that on both devices. Inside onConnected() I request for location updates which then does not result in calling onLocationChanged.Wivern
Did you find a fix or workaround? I'm facing the same problemJosefajosefina
No I didnt....tried everything I could find on the internet but didn't help. I am sure though, that there is some way around it; the way which google maps use or any other google app for that matter...Wivern
Some new progress in this question @Wivern ?Neuroglia
M
6

I observed same behavior on Galaxy Nexus and recognized two things causing onLocationChanged() to not being called. There are

  1. Corresponding location source is not enabled in System Settings. For PRIORITY_HIGH_ACCURACY the GPS satellites must be enabled, for PRIORITY_BALANCED_POWER_ACCURACY Wi-Fi & mobile network location must be enabled.

  2. Even for the enabled location sources there can be no valid location information available. For instance, PRIORITY_HIGH_ACCURACY source is enabled, but there is no (or not good) GPS reception (e.g. device is inside a building). Or PRIORITY_BALANCED_POWER_ACCURACY source is enabled, but both Wi-Fi and mobile data are switched off. In those cases location cannot be detected and onLocationChanged() will not be called.

To fix #1 I had to check whether required location sources are enabled even before connecting to Google Services API and if they are switched off, then I notified a user asked him/her to allow location access.

To solve #2 I decided to use LocationRequest.setExpirationDuration(). I set timeout at about 10 seconds, and immediately after calling requestLocationUpdates() I post a callback delayed by same 10 seconds. If delayed callback is called before requestLocationUpdates(), this means location cannot be detected due to reason #2. I do three retries and then show user an error message.

Android documentation is not great at this place, that's why I hope this helps.

Marylnmarylou answered 7/1, 2015 at 21:27 Comment(1)
could you please explain a little bit more for your #2 solution? I didn't understand it very muchTarrah
P
4
lr.setInterval(100); 

with it your interval is 0,1 second so it very hard for gps chipset to detect location. Your interval should be > 1 second.(with me it is > 10)

locationRequest it should be set:

setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) because PRIORITY_BALANCED_POWER_ACCURACY in outdoor it doesn't get location . Code will look like :

lr=LocationRequest.create();

lr.setInterval(5 * 1000);// 5s

lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationclient.requestLocationUpdates(lr, this);

Hope this help.

Packsaddle answered 8/1, 2015 at 7:17 Comment(2)
I agree. Try replacing PRIORITY_BALANCED_POWER_ACCURACY with PRIORITY_HIGH_ACCURACY as a first step in resolving anomalous behaviour with FusedLocationProviderClient/LocationCallbackReceipt
In my case after changing PRIORITY_BALANCED_POWER_ACCURACY to PRIORITY_HIGH_ACCURACY I was able to know whether GPS is turned on, but lost an ability to call onLocationChanged.Felony
C
0

See if you're getting a passed location by testing for if (loc == null). Even though it's the same provider, maybe one device is coming back null and bombing out, resulting in a look like it's not firing.

Cull answered 7/8, 2013 at 17:17 Comment(1)
Checking that didnt help.Wivern
A
0

This maybe non-sense but I have faced this problem before, someone suggested that you should create your own LocationListener instead of letting your MainActivity implements LocationListener. Basically this:

locationclient.requestLocationUpdates(lr, your_location_listener);

Hope this helps.

Alicaalicante answered 8/8, 2013 at 3:32 Comment(1)
No this too didn't help.Wivern
W
0

I solved this by setting both parameters, minimum distance and minimum time between updates to 0. As per what I have tested on 2 devices, now onLocationChanged() is being called regularly after requesting for updates.

Wivern answered 14/8, 2013 at 9:36 Comment(1)
I started getting problems with this approach too. On some devices, I am unable to get a location fix.Wivern
D
0

try to remove and reinstall "google play services". Install it from google market link bellow, google play services

Doctor answered 27/3, 2014 at 9:29 Comment(0)
S
-1

Here Check this code out...

public class MyActivity extends Activity implements LocationListener {
double destLat, destLong;

LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    destLat = location.getLatitude();
    destLong = location.getLongitude();
}

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

}

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

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
Staffer answered 6/12, 2014 at 11:11 Comment(1)
This example is not using the Fused Location API.Pyrrolidine

© 2022 - 2024 — McMap. All rights reserved.