This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PeriodicWorkRequest locationWork = new PeriodicWorkRequest.Builder( | |
LocationWork.class, 15, TimeUnit.MINUTES).addTag(LocationWork.TAG).build(); | |
WorkManager.getInstance().enqueue(locationWork); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocationWork extends Worker { | |
... | |
public WorkerResult doWork() { | |
Log.d(TAG, "doWork: Started to work"); | |
handlerThread = new HandlerThread("MyHandlerThread"); | |
handlerThread.start(); | |
looper = handlerThread.getLooper(); | |
locationTracker = new LocationTracker(getApplicationContext(), looper); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Constraints constraints = new Constraints.Builder().setRequiredNetworkType | |
(NetworkType.CONNECTED).build(); | |
PeriodicWorkRequest locationWork = new PeriodicWorkRequest.Builder(LocationWork | |
.class, 15, TimeUnit.MINUTES).addTag(LocationWork.TAG) | |
.setConstraints(constraints).build(); | |
WorkManager.getInstance().enqueue(locationWork); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WorkManager.getInstance().getStatusById(locationWork.getId()).observe(this, | |
workStatus -> { | |
if(workStatus!=null && workStatus.getState().isFinished()){ | |
... | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WorkManager.getInstance().getStatusById(locationWork.getId()).observe(this, | |
workStatus -> { | |
if(workStatus!=null && workStatus.getState().isFinished()){ | |
... | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WorkManager.getInstance(this).enqueue(Work.from(LocationWork.class, | |
LocationUploadWorker.class)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WorkManager.getInstance(this) | |
.beginWith(Work.from(LocationWork.class)) | |
.then(Work.from(LocationUploadWorker.class)) | |
.enqueue(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |