How to update GPS Location on App Background to Fo

2019-09-17 09:03发布

问题:

I am using GPS either GPS_PROVIDER or NETWORK_SERVICE_PROVIDER for latitude and longitude . I want to detect when specifically App goes background to foreground (Either By Home button) .

ISSUES:

  1. GPS detected location is not updated from time to time until any app used it so can get the updated location .
  2. Detect the Background to foreground of app and detect location without battery issues .
  3. Any GPS location update time to time without any battery issues.

回答1:

For get location in background frequently use FusedLocation provider in a android service class.

Fused location provide give more accurate location and consume less power.

public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener{

private static final String TAG = "LocationService";
private static final int LOCATION_INTERVAL = 1000;
private Context mContext;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private FusedLocationProviderApi fusedLocationProviderApi;

@Override
public IBinder onBind(Intent arg0){
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);       
    return START_STICKY;
}

@Override
public void onCreate(){
    Log.e(TAG, "onCreate");
    mContext = this;
    getLocation();
}

@Override
public void onDestroy(){
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    try{
        if(googleApiClient!=null){
            googleApiClient.disconnect();
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

} 

private void getLocation(){
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(LOCATION_INTERVAL);
    locationRequest.setFastestInterval(LOCATION_INTERVAL);
    fusedLocationProviderApi = LocationServices.FusedLocationApi;
    googleApiClient = new GoogleApiClient.Builder(this)
    .addApi(LocationServices.API)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    if (googleApiClient != null) {
        googleApiClient.connect();
      }
  }

 @Override
public void onConnected(Bundle arg0) {
//  Location location = fusedLocationProviderApi.getLastLocation(googleApiClient);
    fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

@Override
public void onConnectionSuspended(int arg0) {

}

@Override
public void onResponse(int reqCode, int statusCode, String json) {

}

@Override
public void onCancel(boolean canceled) {

}

@Override
public void onProgressChange(int progress) {

}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(mContext, "Driver location :"+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {

}
}

and call service from your activity-

    startService(new Intent(mContext,LocationService.class));

hope it will help you.

EDITED

Also add below code to your manifest -

    <service 
         android:name=".LocationService">
     </service>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>