Skip to content

Instantly share code, notes, and snippets.

Avatar

Yonatan Levin parahall

View GitHub Profile
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);
View WorkManager.java
@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) {
View MainActivity.java
WorkManager.getInstance(this).enqueue(Work.from(LocationWork.class,
LocationUploadWorker.class));
View MainActivity.java
WorkManager.getInstance(this)
.beginWith(Work.from(LocationWork.class))
.then(Work.from(LocationUploadWorker.class))
.enqueue();
View MainActivity.java
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);
View MainActivity.java
WorkManager.getInstance(context).getStatusById(work.getId())
.observe(lifecycleOwner, status -> {
// Do something with the status
if (status != null && status.getState().isFinished()) { … }
});
View MainActivity.java
//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);
View LocationUploadWorker.java
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;
View JobDispatcherScheduler.java
Job myJob = firebaseJobDispatcher.newJobBuilder()
.setService(SmartService.class)
.setTag(SmartService.LOCATION_SMART_JOB)
.setReplaceCurrent(false)
.setConstraints(ON_ANY_NETWORK)
.build();
firebaseJobDispatcher.mustSchedule(myJob);
View JobSchdeuler.java
ComponentName service = new ComponentName(this, MyJobService.class);
JobScheduler mJobScheduler = (JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(jobId, serviceComponent)
.setRequiredNetworkType(jobInfoNetworkType)
.setRequiresCharging(false)
.setRequiresDeviceIdle(false)
.setExtras(extras).build();
mJobScheduler.schedule(jobInfo);
You can’t perform that action at this time.