Skip to content

Instantly share code, notes, and snippets.

WorkManager.getInstance().getStatusById(locationWork.getId()).observe(this,
workStatus -> {
if(workStatus!=null && workStatus.getState().isFinished()){
...
}
});
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType
.CONNECTED).build();
Data inputData = new Data.Builder()
.putDouble(LocationUploadWorker.LOCATION_LAT, location.getLatitude())
.putDouble(LocationUploadWorker.LOCATION_LONG, location.getLongitude())
.putLong(LocationUploadWorker.LOCATION_TIME, location.getTime())
.build();
OneTimeWorkRequest uploadWork = new OneTimeWorkRequest.Builder(LocationUploadWorker.class)
.setConstraints(constraints).setInputData(inputData).build();
WorkManager.getInstance().enqueue(uploadWork);
public class LocationUploadWorker extends Worker {
...
//Upload last passed location to the server
public WorkerResult doWork() {
ServerReport serverReport = new ServerReport(getInputData().getDouble(LOCATION_LONG, 0),
getInputData().getDouble(LOCATION_LAT, 0), getInputData().getLong(LOCATION_TIME,
0));
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef =
database.getReference("WorkerReport v" + android.os.Build.VERSION.SDK_INT);
@Override
public WorkerResult doWork() {
...
LocalBroadcastManager.getInstance(getAppContext()).registerReceiver(receiver, new IntentFilter(LocationBaseBroadcast.ACTION_NEW_LOCATION_ARRIVED));
locationTracker.start();
try {
locationWait = new CountDownLatch(1);
locationWait.await();
} catch (InterruptedException e) {
WorkManager.getInstance(this).enqueue(Work.from(LocationWork.class,
LocationUploadWorker.class));
WorkManager.getInstance(this)
.beginWith(Work.from(LocationWork.class))
.then(Work.from(LocationUploadWorker.class))
.enqueue();
Constraints constraints = new Constraints.Builder().setRequiredNetworkType
(NetworkType.CONNECTED).build();
PeriodicWork locationWork = new PeriodicWork.Builder(LocationWork.class,
FIVETEEN_MIN, TimeUnit.MILLISECONDS).addTag(LocationWork.TAG)
.withConstraints(constraints).build();
WorkManager.getInstance(this).enqueue(locationWork);
WorkManager.getInstance(context).getStatusById(work.getId())
.observe(lifecycleOwner, status -> {
// Do something with the status
if (status != null && status.getState().isFinished()) { … }
});
//Create constraints
Constraints constraints = new Constraints.Builder().setRequiredNetworkType
(NetworkType.CONNECTED).build();
//create work
Work uploadWork = new Work.Builder(LocationUploadWorker.class).withConstraints
(constraints).build();
//schedule work
WorkManager.getInstance(this).enqueue(uploadWork);
public class LocationUploadWorker extends Worker {
@Override
public WorkerResult doWork() {
Location location = LocationRepository.getLastLocation();
ServerReport serverReport = new ServerReport(location);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef =
database.getReference("WorkerReport v" + android.os.Build.VERSION.SDK_INT);
myRef.push().setValue(serverReport);
return WorkerResult.SUCCESS;