Skip to content

Instantly share code, notes, and snippets.

Yonatan Levin parahall

Block or report user

Report or block parahall

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View MainViewModel.java
PeriodicWorkRequest locationWork = new PeriodicWorkRequest.Builder(
LocationWork.class, 15, TimeUnit.MINUTES).addTag(LocationWork.TAG).build();
WorkManager.getInstance().enqueue(locationWork);
View LocationUploadWorker.java
public class LocationUploadWorker extends Worker {
//...
public WorkerResult doWork() {
//get Data out from input
double longitude = getInputData().getDouble(LOCATION_LONG, 0);
double latitude = getInputData().getDouble(LOCATION_LAT, 0);
long time = getInputData().getLong(LOCATION_TIME, 0);
String osVersion = "WorkerReport v" + Build.VERSION.SDK_INT;
//construct our report for server format
View LocationTracker.java
//location obtained, need to send it to server
private void broadcastLocation(Location location) {
//release latch
reportFinished();
//We need to sure that device have internet live
Constraints constraints = new Constraints.Builder().setRequiredNetworkType
(NetworkType.CONNECTED).build();
//Parse our location to Data to use it as input for our worker
Data inputData = new Data.Builder()
View LocationWork.java
public class LocationWork extends Worker {
...
static void reportFinished() {
if (locationWait != null) {
Log.d(TAG, "doWork: locationWait down by one");
locationWait.countDown();
}
}
View LocationWork.java
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);
View MainActivity.java
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);
View MainActivity.java
WorkManager.getInstance().getStatusById(locationWork.getId()).observe(this,
workStatus -> {
if(workStatus!=null && workStatus.getState().isFinished()){
...
}
});
View MainActivity.java
WorkManager.getInstance().getStatusById(locationWork.getId()).observe(this,
workStatus -> {
if(workStatus!=null && workStatus.getState().isFinished()){
...
}
});
View MainActivity.java
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);
View LocationUploadWorker.java
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);
You can’t perform that action at this time.