GPS Location tracking even when app is closed(Not running in Background)/ScreenLocked

烈酒焚心 提交于 2019-12-02 22:07:56

问题


I want to track user location very similar to strava, even after the is closed.

I tried AlarmManager but it's not giving me execution after every one minute


回答1:


Just using a Service as BiGGZ explained won't work in case the device enters sleep mode. The Service won't be killed though but your app won't get any CPU. Therefore you would have to acquire 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();
}

If you need Location updates less frequently you could use the AlarmManager and set a repeating alarm which triggers a Location update. The alarm should trigger then a BroadcastReceiver, since it's not guaranteed that a Service is successfully started before the device goes back to sleep again. See here: https://developer.android.com/reference/android/app/AlarmManager.html




回答2:


Use a 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);
}
}


来源:https://stackoverflow.com/questions/41023986/gps-location-tracking-even-when-app-is-closednot-running-in-background-screenl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!