0

I am using google location api for fetching location. issue I'm facing is when the user turned the GPS off I'm not getting the Lcoation, as per this post says we can access Network based location even-though the GPS is turned off. but I'm not getting locaton when the user turned off the location access.

this is my service to fetch location

public class LocationUpdateService extends Service implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<Status> {


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startLocationUpdates();
    return START_STICKY;
}


@Override
public void onCreate() {
    super.onCreate();
    LogUtils.LOGI(TAG, "Service created");
    buildGoogleApiClient();
    setupBroadCastListenerForGpsStatusChange();
    initLastLocationDetails();
}


/**
 * Requests location updates from the FusedLocationApi.
 */
protected void startLocationUpdates() {
    if (!mGoogleApiClient.isConnected()) {
        mGoogleApiClient.connect();
    } else {
        mRequestingLocationUpdates = true;
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
    }
}

/**
 * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the
 * LocationServices API.
 */
protected synchronized void buildGoogleApiClient() {
    LogUtils.LOGI(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .addApi(ActivityRecognition.API)
            .build();
    createLocationRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setSmallestDisplacement(SMALLEST_DISTANCE_METERS);
}

  @Override
    public void onConnected(Bundle connectionHint) {
        startLocationUpdates();
    }

   @Override
    public void onLocationChanged(Location location) {
      //location operations 
   }     
}

So my question is, Is it really possible to access location when the location access is disabled (not last cached location), if yes, what is wrong with my code

Community
  • 1
  • 1
droidev
  • 7,352
  • 11
  • 62
  • 94

2 Answers2

1
 if (isNetworkEnabled) {
   manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATES,
                            DISTANCE_OF_UPDAPTES, this);
     if (manager != null) {
     location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
       if (location != null) {
          latitude = location.getLatitude();
          longitude = location.getLongitude();
       }
    }
}
droidev
  • 7,352
  • 11
  • 62
  • 94
sanket
  • 77
  • 2
  • 1
    please read the question carefully before answering, see I'm using google play location api, and you are posting answer for android native location api – droidev Mar 02 '16 at 04:37
0

You canot start the service natively using just Google Api Location , you should manage to operate the location based on user request by using boolean location manager. Otherwise your attempts dose not look superficial, you can either use get when gps is on or off depending when detected.

Hashes
  • 110
  • 8
  • i dint unserstand, can you explain more ? – droidev Mar 02 '16 at 05:07
  • Follow patel milan answer , this should guide you for further explanation , your code attributes attempt to authenticate a Google Based API request!! Based on! You have to define what to request and whom request to whom , and that is achievable by identifying location manager and location based on permission. – Hashes Mar 02 '16 at 05:26