Android 7 Jobscheduler Get Event When New Picture Is Taken By The Camera
I have a problem on Android 7 that not support the broadcast event 'android.hardware.action.NEW_PICTURE' longer. I write now for Android 7 a JobService but it will not fire when a
Solution 1:
In the below code you can pass the flag immediate
as false for normal operation (i.e. schedule within system guidelines for an app's good behaviour). When your app's main activity starts you can pass immediate
as true to force a quick retrieval of media content changes.
You should run code in the onStartJob()
method in a background job. (As shown below.)
If you only want to receive media from the camera and not other sources you should just filter out URI's based on their path. So only include "*/DCIM/*"
. (I haven't put this in the below code though.)
Also the Android job scheduler has a policy where it denies your service if it detects over abuse. Maybe your tests have caused this in your app, so just uninstall and reinstall to reset it.
publicclassZNJobServiceextendsJobService {
//...finalHandlerworkHandler=newHandler();
Runnable workRunnable;
//...publicstaticvoidregisterJob(Context context, boolean immediate) {
finalJobInfojobInfo= createJobInfo(context, immediate);
finalJobSchedulerjs= context.getSystemService(JobScheduler.class);
finalintresult= js.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS) {
log.INFO(" JobScheduler OK");
} else {
log.ERROR(" JobScheduler fails");
}
}
privatestatic JobInfo createJobInfo(Context context, boolean immediate) {
final JobInfo.Builderb=newJobInfo.Builder(
ZNJOBSERVICE_JOB_ID, newComponentName(context, ZNJobService.class));
// Look for specific changes to images in the provider.
b.addTriggerContentUri(
newJobInfo.TriggerContentUri(
MediaStore.Images.Media.INTERNAL_CONTENT_URI,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
b.addTriggerContentUri(
newJobInfo.TriggerContentUri(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
if (immediate) {
// Get all media changes within a tenth of a second.
b.setTriggerContentUpdateDelay(1);
b.setTriggerContentMaxDelay(100);
} else {
// Wait at least 15 minutes before checking content changes.// (Change this as necessary.)
b.setTriggerContentUpdateDelay(15 * 60 * 1000);
// No longer than 2 hours for content changes.// (Change this as necessary.)
b.setTriggerContentMaxDelay(2 * 60 * 60 * 1000);
}
return b.build();
}
@OverridepublicbooleanonStartJob(final JobParameters params) {
log.INFO("onStartJob");
if (params.getTriggeredContentAuthorities() != null && params.getTriggeredContentUris() != null) {
// Process changes to media content in a background thread.
workRunnable = newRunnable() {
@Overridepublicvoidrun() {
yourMethod(params.getTriggeredContentUris());
// Reschedule manually. (The 'immediate' flag might have changed.)
jobFinished(params, /*reschedule*/false);
scheduleJob(ZNJobService.this, /*immediate*/false);
}};
Postal.ensurePost(workHandler, workRunnable);
returntrue;
}
// Only reschedule the job.
scheduleJob(this, /*immediate*/false);
returnfalse;
}
@OverridepublicbooleanonStopJob(final JobParameters params) {
if (workRunnable != null) {
workHandler.removeCallbacks(workRunnable);
workRunnable = null;
}
returnfalse;
}
privatestaticvoidyourMethod(Uri[] uris) {
for (Uri uri : uris) {
log.INFO("JobService Uri=%s", uri.toString());
}
}
}
Post a Comment for "Android 7 Jobscheduler Get Event When New Picture Is Taken By The Camera"