Skip to content

Instantly share code, notes, and snippets.

View MyService.java
public class MyService extends Service {
private static final String TAG = MyService.class.getSimpleName();
private ArrayList<Object> objects;
public MyService() {
}
@Override public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
View MyIntentService.java
public class MyIntentService extends IntentService {
private static final String TAG = MyIntentService.class.getSimpleName();
private ArrayList<Object> objects;
private long timeMillisInBackground;
public MyIntentService() {
super("MyIntentService");
}
@Override protected void onHandleIntent(Intent intent) {
View AndroidManifest.xml
<receiver
android:name=".login.NetworkHttpUnauthorizedReceiver">
<intent-filter>
<action android:name="com.startup.network.http_unauthorized"/>
</intent-filter>
</receiver>
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);
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 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 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
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(this)
.beginWith(Work.from(LocationWork.class))
.then(Work.from(LocationUploadWorker.class))
.enqueue();
View MainActivity.java
WorkManager.getInstance(this).enqueue(Work.from(LocationWork.class,
LocationUploadWorker.class));