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