Now I'm using Firebase for uploading photos and this working great and was able to rebuild this work to use workmanager, but I don't know how to pass content URI trigger to my workmanager builder.
val dispatcher = FirebaseJobDispatcher(GooglePlayDriver(applicationContext))
val job = dispatcher.newJobBuilder()
.setService(UploadJobService::class.java)
.setTag(TAG)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(0, 60))
.setTrigger(Trigger.contentUriTrigger( Arrays.asList(
ObservedUri(Uri.parse(Environment.getExternalStorageDirectory().absolutePath), ObservedUri.Flags.FLAG_NOTIFY_FOR_DESCENDANTS))
))
.setLifetime(Lifetime.FOREVER)
.setReplaceCurrent(false)
.setConstraints(
Constraint.ON_UNMETERED_NETWORK
)
.build()
dispatcher.mustSchedule(job)
Here is code what I use for workmanager and it works most of the time, but when I take the photo and delete this is not working, due 15min interval, How I can register new unique worker what will start the job when the new photo is detected?
EDIT:Here is code what im using and its working, but not sure if i implement correctly...
This is how to schedule worker:
public static void Checkfornewphotos(String ONE_MY_WORK) {
OneTimeWorkRequest.Builder photoCheckBuilder =
new OneTimeWorkRequest.Builder(MyWorker.class);
photoCheckBuilder.setConstraints(new Constraints.Builder()
.addContentUriTrigger(MediaStore.Images.Media.INTERNAL_CONTENT_URI, true)
.addContentUriTrigger(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true)
.build());
OneTimeWorkRequest Photocheck = photoCheckBuilder.build();
WorkManager instance = WorkManager.getInstance();
instance.enqueueUniqueWork(ONE_MY_WORK, ExistingWorkPolicy.REPLACE, Photocheck);
}
Here is my worker:
public class MyWorker extends Worker {
public MyWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
}
@Override
public Worker.Result doWork() {
// Do your actual work
try {
Log.i(TAG, "mywork")
Result.RETRY
} catch (exception: Exception) {
Log.i(TAG, "mywork")
Result.SUCCESS
}
// Then start listening for more changes
Checkfornewphotos(getTags().iterator().next());
}
}