0

Is it possible to get the location from NETWORK if GPS is OFF ?

My Demo can get the current user-location from NETWORK and from GPS but only if GPS is ON.

If I try to get the location from LastKnownLocation, I get null too.

 @Override
 protected void onResume() {
 super.onResume();
        
 if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            startActivity(new Intent(MainActivity.this, Permissions.class));
            return;
        }
    

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    isGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
    if (!isGPS && !isNetwork) {
Log.d("TAG", "gps and network false");
           
}else {
if (isGPS) {
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
Log.i("TAG", "gps loc found");}
}

if (isNetwork) {
loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc != null) {
Log.i("TAG", "network loc found");
}
}

I have tested this code below. it returns the location null too, if I make the device-gps off : GPSTracker.java

Tryer
  • 3
  • 2
  • Can you post the code where you retrieve the location? Also, probably similar question here: https://stackoverflow.com/questions/20210565/android-location-manager-get-gps-location-if-no-gps-then-get-to-network-provid – jordansilva Nov 20 '20 at 22:40
  • I have updated the code to show you how I retrieve the loc. – Tryer Nov 20 '20 at 23:26
  • I have tested this GPSTracker. it returns null too if gps is off : [GPSTracker.java](https://gist.github.com/ashmeh6/dfa95b3a386e78ac36292566298c7839) – Tryer Nov 22 '20 at 17:02

1 Answers1

0

If you are trying to request the location from the network as a fallback to GPS, you will need to have ACCESS_COARSE_LOCATION enabled. Also, even with the permission enabled, you must request a location update, to refresh and update the last network position. Only after that, you'll have the last known location available, otherwise, you'll get null indeed.

To make your code work, you probably just need to call your GPSTracker.getLocation() before your line with getSystemService(Context.LOCATION_SERVICE);

You can check the documentation for getLastKnownLocation here: https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)

Gets the last known location from the given provider, or null if there is no last known location. The returned location may be quite old in some circumstances, so the age of the location should always be checked.

  This will never activate sensors to compute a new location, and will only ever return a cached location.

jordansilva
  • 182
  • 1
  • 1
  • 13