1

I read posts like this, android getlastknownlocation returns null, but I have a problem apart from the known location.

i know that if i don't have any provider, i can't have any known location, getLastKnowedLocation(). but, How i can get the location when the provider is detected?

i use this code:

 public void myPosition() {
    if (mapa.getMyLocation() != null)
        mapa.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng( mapa.getMyLocation().getLatitude(), mapa.getMyLocation().getLongitude()), 13));
    else{
        loca = locaMan.getLastKnownLocation(bestProvi);

        if (loca != null)
        mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng( loca.getLatitude(), loca.getLongitude()), 15));

    }
}
public void onLocationChanged(Location location) {
    myPosition();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    myPosition();
}

@Override
public void onProviderEnabled(String provider) {
    myPosition();
    Toast toast1 =Toast.makeText(getApplicationContext(),"Provider ON", Toast.LENGTH_SHORT);
    toast1.show();
}

@Override
public void onProviderDisabled(String provider) {
    Toast toast1 =Toast.makeText(getApplicationContext(),"Provider OFF", Toast.LENGTH_SHORT);
    toast1.show();
}

But when the provider is activated, it isn't execute the code of method onProviderEnabled(), only when i turn off and turn on GPS on my smartphone.

When i start the aplication, i see a map, and 5 seconds after i see a blue point over my position, but myPosition() method isn't executed.

How i can do this?

Thank you for all.

  • *myPosition() method isn't executed.* how do you verify? – Tim Jul 28 '15 at 15:01
  • At first it is executed in onCreate (), but not finding any provider, any change that does not comply any "if". But in the part of OnStatusChanged () or onProviderEnabled () it does nothing. Although on the map the blue point on my position appear after five seconds, it not detected as a change or a provider activation. All this is verified with Android Studio debugger. – Iratzar Carrasson Bores Jul 28 '15 at 19:25

1 Answers1

0

I was trying to search something about that and i found the metod requestLocationUpdates() in these links: 1 2 (Spanish), but it didn't work in my project.

So i had to search more but from other way, because my problem wasn't the location, but the camera movement to this location. i found the TimerTask library and i used it.

I add this code into the other code:

 public class TimerClass extends TimerTask{
        public void run () {
            runOnUiThread(new Runnable() {
                @Override
                public void run () {
                    if(!pass) {
                        myPosition();
                    }
                }
            });
        }
}

it is a class into the MainActivity class.

i don't know if it is correct, but it runs without any problem. If someone know other way for fix this problem, answer this comment please.