ahmed osama
ahmed osama

Reputation: 295

Android Fails to get location

I am trying to get location of the virtual device AVD the problem here that the location return by null , i have added the permission to the app and still failed to get location

public class MainActivity extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    provider = locationManager.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                      int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.

        Log.i("Location Info","Permission Denied");
        return;
    }
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
        Log.i("Location Info","Location Achieved ");

    } else {

        Log.i("Location Info", "Location Failed");
    }

}

@Override
protected void onResume() {
    super.onResume();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
      Double longitude = location.getLongitude();
      Double latitude = location.getLatitude();
      Log.i("location Info Lat",longitude.toString());
      Log.i("Location Info long",longitude.toString());
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

the result is all the time log info :Location Failed , what is the solution in this case

Upvotes: 0

Views: 239

Answers (2)

ahmed osama
ahmed osama

Reputation: 295

The problem here was as mentioned by @Florescu it starts from unknown state when i give the emulator the dimension for the first time , find out the emulator understand this as a movement but when i run it again it gives me the correct behavior

Upvotes: 0

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

Location is null on emulator because it doesen't know your last sent location (fresh start or something else). You need to send your coordinates to emulator. Works better when you test on a real device.

Edit:

Enter coordinates like in this image.

Upvotes: 1

Related Questions