GPS location tracking, even when the application is closed (does not work in the background) / ScreenLocked

I want to track the user's location very much like strava, even after closing.

I tried AlarmManager but it did not give me execution every minute

0
source share
2 answers

Use the Service:

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

private final static int UPDATE_INTERVAL = 300000;//whatever you want
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public UpdateLocationService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return START_STICKY;
}


@Override
public void onCreate() {
    super.onCreate();
    buildGoogleApiClient();
}

synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(UpdateLocationService.this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

}

@Override
public void onConnected(@Nullable Bundle bundle) {

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);

    try{
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, UpdateLocationService.this);

        Toast.makeText(UpdateLocationService.this, "onConnected", Toast.LENGTH_SHORT).show();
    }catch(SecurityException e){

    }
}


@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
    Toast.makeText(UpdateLocationService.this, "onLocationChanged", Toast.LENGTH_SHORT).show();

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Toast.makeText(UpdateLocationService.this, "onConnectionFailed", Toast.LENGTH_SHORT).show();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onDestroy() {
    super.onDestroy();
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, UpdateLocationService.this);

}

@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, UpdateLocationService.this);
}
}
+2
source

Simply using the service described in BiGGZ will not work if the device goes into sleep mode. The service will not be killed, but your application will not receive any processor. Therefore, you will have to purchase a partial Wakelock to prevent the device from entering sleep mode.

@Override
public void onCreate() {
  super.onCreate();
  PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
  wakeLock = m.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Some Tag");
  wakeLock.acquire();
  ...
}

@Override
public void onDestroy() {
  wakeLock.release();
}

, AlarmManager , . BroadcastReceiver, , , . . : https://developer.android.com/reference/android/app/AlarmManager.html

+2

Source: https://habr.com/ru/post/1671856/


All Articles