Search code examples
androidgpslocationlocationmanagerlocationlistener

GPS location provider gives me null location in Android


I try to find location using GPS_Provider in android. when try to get location using Network_Provider than it give me location and call callback function properly, but when I tried to get location using GPS_Provider than it give me location null & also not calling callback function onLocationChanged of LocationListener.

My code is:

 LocationManager lm;
    boolean isNetwork = false;
    Location location;
    LocationListener locationListener;
    LocationListener loc;
    MyLocation myLocation;
    Location location1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        try {
            myLocation = new MyLocation();

            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
                Log.e("Location", "Permission Not Given");
                return;
            }
            Log.e("Location", "Permission is Given");
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocation);
            location1=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }catch(Exception e)
        {

        }
    }

MyLocation.class

public class MyLocation implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {

        if(location!=null)
        {

            String lat,Longi;
            lat=String.valueOf(location.getLatitude());
            Longi=String.valueOf(location.getLongitude());

            Log.e("Latitude: " , lat);
            Log.e("Longitude: " , Longi);

        }
        else
            Log.e("Location is null", "Location not Found");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e("Latitude: " ,""+ provider+ " Status: "+status);
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e("Latitude: " , provider);
    }
}

It give me logcat error as follow:

02-04 00:50:52.280 14480-14480/com.arc.locationusingnetwork E/Location: Permission is Given
02-04 00:50:52.360 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.390 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52

Solution

  • The old LocationListener class is probably deprecated. You should use FusedLocationApi by Android to fetch current location or receive location updates via FusedLocationAPI's LocationListener.

    It's a good tutorial for using FusedLocationApi : Android Location API