LocationClient requestUpdates onLocationChanged never called

I have this code that I use to host the map application. I tested it on a Galaxy S2 device and worked great, but when I tested it on a nexus 4 device, callback onLocationChanged never called. What can i do wrong?

Here is my activity code:

  public class BaseMapActivity extends FragmentActivity implements Injectable, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener { private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; private MapEntity component; private boolean yetStarted = false; private LocationClient mLocationClient; private LocationRequest mLocationRequest; private Location location; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_activity); mLocationClient = new LocationClient(this, this, this); mLocationRequest = LocationRequest.create(); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // TODO mLocationRequest.setInterval(5000); // TODO mLocationRequest.setFastestInterval(5000); // TODO replaceFragment(R.id.map_container, mapFragment); // Agrega el fragment de extras Fragment extrasFragment = component.getExtrasFragment(); replaceFragment(R.id.map_extras, extrasFragment); } @Override protected void onStart() { super.onStart(); mLocationClient.connect(); } @Override protected void onStop() { if (mLocationClient.isConnected()) { mLocationClient.removeLocationUpdates(this); mLocationClient.disconnect(); } super.onStop(); } @Override public void onConnected(Bundle arg0) { mLocationClient.requestLocationUpdates(mLocationRequest, this); location = mLocationClient.getLastLocation(); if (location == null) { return; } Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show(); } @Override public void onDisconnected() { mLocationClient.removeLocationUpdates(this); Toast.makeText(this, "onDisconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); } @Override public void onLocationChanged(Location location) { component.setCenter(location); String msg = "Updated Location: " + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case CONNECTION_FAILURE_RESOLUTION_REQUEST: switch (resultCode) { case Activity.RESULT_OK: break; } } } @SuppressLint("ShowToast") @Override public void onConnectionFailed(ConnectionResult connectionResult) { if (connectionResult.hasResolution()) { try { connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } else { Toast.makeText(this, "An error ocurred getting current location", Toast.LENGTH_SHORT); } } } 

It is strange that it works on one device, but not in another.

+6
source share
1 answer

Try setting the location layout to see if locationChange is being called or not.

Here you have an example that I used to do this before production.

 private Location createMockLocation() { if (mockLocation == null) { mockLocation = new Location("mock"); mockLocation.setLatitude(41.4934133); mockLocation.setLongitude(-23.8729252); mockLocation.setAccuracy(1.0f); mockLocation.setTime(new Date().getTime()); } else { mockLocation.setLatitude(mockLocation.getLatitude() + 0.00003); mockLocation.setLongitude(mockLocation.getLongitude() + 0.00001); mockLocation.setTime(new Date().getTime()); } return mockLocation; } private void mockLocation() { LocationServices.FusedLocationApi.setMockMode(clientApi, true); LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation()); final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { public void run() { LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation()); // Log.d("MockLocation", // "mockLocation: Lat:"+mockLocation.getLatitude()+" Long:"+mockLocation.getLongitude()); } }, 0, NORMAL_INTERVAL, TimeUnit.MILLISECONDS); } 

Using this method, you get the update position, each NORMAL_INTERVAL in ms.

Hope this helps :).

0
source

Source: https://habr.com/ru/post/950274/


All Articles