Last active
March 2, 2019 15:18
-
-
Save adavis/6e54e3ce48b4090ecd5e352f3ef44b09 to your computer and use it in GitHub Desktop.
Background Jobs with Android Job and Dagger
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
@Singleton | |
@Component( | |
modules = { | |
AppModule.class, JobsModule.class | |
} | |
) | |
public interface AppComponent | |
{ | |
Application getApplication (); | |
JobManager getJobManager (); | |
} |
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
@Singleton | |
public class AppJobCreator implements JobCreator | |
{ | |
@Inject | |
Map<String, Provider<Job>> jobs; | |
@Inject | |
AppJobCreator () | |
{ | |
} | |
@Override | |
public Job create (String tag) | |
{ | |
Provider<Job> jobProvider = jobs.get( tag ); | |
return jobProvider != null ? jobProvider.get() : null; | |
} | |
} |
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
@Singleton | |
public class BookService | |
{ | |
private final JobManager jobManager; | |
@Inject | |
public BookService (JobManager jobManager) | |
{ | |
this.jobManager = jobManager; | |
} | |
void submitBook (String name) | |
{ | |
jobManager.schedule( SendBookRequestJob.buildJobRequest( name ) ); | |
} | |
} |
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
// Job Scheduler | |
compile 'com.evernote:android-job:1.1.3' | |
compile "com.google.android.gms:play-services-gcm:$playServicesVersion" |
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
@Module | |
public class JobsModule | |
{ | |
@Provides | |
@Singleton | |
JobManager provideJobManager (Application application, AppJobCreator jobCreator) | |
{ | |
JobManager.create( application ).addJobCreator( jobCreator ); | |
return JobManager.instance(); | |
} | |
@Provides @IntoMap | |
@StringKey( SendBookRequestJob.JOB_TAG ) | |
Job provideSendBookRequestJob (BookApi api, Bus bus) | |
{ | |
return new SendBookRequestJob( api, bus ); | |
} | |
} |
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 SendBookRequestJob extends Job | |
{ | |
static final String JOB_TAG = "send_book_request_tag"; | |
private static final String PARAM_BOOK_NAME = "book_name"; | |
private final BookApi api; | |
private final Bus bus; | |
@Inject | |
SendBookRequestJob (BookApi api, Bus bus) | |
{ | |
this.api = api; | |
this.bus = bus; | |
} | |
@NonNull | |
@Override | |
protected Result onRunJob (final Params params) | |
{ | |
PersistableBundleCompat extras = params.getExtras(); | |
String name = extras.getString( PARAM_BOOK_NAME, "" ); | |
if ( submitRequest( name ) ) | |
{ | |
return Result.SUCCESS; | |
} | |
return Result.FAILURE; | |
} | |
boolean submitRequest (String name) | |
{ | |
api.submitBook( name ).enqueue( new Callback<Book>() | |
{ | |
@Override | |
public void onResponse (Call<Book> call, Response<Book> response) | |
{ | |
if ( response.isSuccessful() ) | |
{ | |
bus.post( new SubmissionSuccessEvent( response.body() ) ); | |
} | |
} | |
@Override | |
public void onFailure (Call<Place> call, Throwable t) | |
{ | |
bus.post( new SubmissionErrorEvent() ); | |
} | |
} ); | |
return true; | |
} | |
public static JobRequest buildJobRequest (String name) | |
{ | |
PersistableBundleCompat extras = new PersistableBundleCompat(); | |
extras.putString( PARAM_BOOK_NAME, name ); | |
return new JobRequest.Builder( SendBookRequestJob.JOB_TAG ) | |
.setExecutionWindow( 5_000L, 10_000L ) | |
.setBackoffCriteria( 20_000L, JobRequest.BackoffPolicy.EXPONENTIAL ) | |
.setRequiredNetworkType( JobRequest.NetworkType.CONNECTED ) | |
.setExtras( extras ) | |
.setRequirementsEnforced( true ) | |
.setPersisted( true ) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example. When I use this approach on my project I get the following exception, when i run the job for second time: