LastKnownLocation always returns null. how can i fix this?

27 Views Asked by At

Location keeps returning null even after accessing my location via the virtual device.

if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_CODE);
}

Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if(location != null){
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    //method to provide the cityName
    cityName = getCityName(longitude,latitude);
    getWeatherInfo(cityName);
}else{
    System.out.println("Location is null! ");
    Toast.makeText(MainActivity.this, "Couldn't get provider",Toast.LENGTH_SHORT)
            .show();
}
0

There are 0 best solutions below