If you read android documentation work manager is not a good way to schedule recurring tasks and when the device goes to sleep mode the thread work manager is working on is also put to sleep to conserve battery try using handler instead.
https://developer.android.com/reference/android/os/Handler
in the code i have posted i have used handler and firebase jobdispatcher to log user location on fixed interval of 30 seconds and it works even when device is in sleep mode
//file with handler and firebase job dispatcher
public class GetterService extends JobService {
private HandlerThread handlerThread = new HandlerThread("HandlerThread");
private Handler threadHandler;
int delay = 30*1000;
Runnable runnable;
LocationManager locationManager;
@Override
public boolean onStartJob(@NonNull com.firebase.jobdispatcher.JobParameters job) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
handlerThread.start();
threadHandler = new Handler();
threadHandler.postDelayed(runnable= () -> {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location repeatedLocations = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.i("location logger", "getting Longitude as "+ repeatedLocations.getLongitude() +" getting Latitide as "+repeatedLocations.getLatitude());
threadHandler.postDelayed(runnable,delay);
},delay);
return false;
}
@Override
public boolean onStopJob(@NonNull com.firebase.jobdispatcher.JobParameters job) {
return false;
}
}
//file with location getter
public class LocationGetter extends AppCompatActivity implements LocationListener {
LocationManager locationManager;
@BindView(R.id.latitudeVal)
TextView latitudeVal;
@BindView(R.id.longitudeVal)
TextView longitudeVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher
(new GooglePlayDriver(getApplicationContext()));
Job notificationJob = dispatcher.newJobBuilder()
.setService(GetterService.class)
.setRecurring(true).setTag("jid")
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
.setTrigger(Trigger.executionWindow(1, 10000))
.setReplaceCurrent(false)
.build();
dispatcher.mustSchedule(notificationJob);
}
@OnClick(R.id.ping)
public void onPingClick() {
}
@Override
public void onLocationChanged(Location location) {
latitudeVal.setText("" + location.getLatitude() + "");
longitudeVal.setText("" + location.getLongitude() + "");
Log.i("dfjh", "" + location.getLongitude() + " " + location.getLatitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}