gpt4 book ai didi

android - 如何使用 Workmanager 检测新照片

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:30 31 4
gpt4 key购买 nike

现在我正在使用 Firebase 上传照片,这很好用,并且能够重建这项工作以使用 workmanager,但我不知道如何将内容 URI 触发器传递给我的 workmanager 构建器。

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)

这是我用于 workmanager 的代码,它大部分时间都有效,但是当我拍照并删除时,它不起作用,因为间隔为 15 分钟,我如何注册新的独特工作人员,当检测到新照片时,什么将开始工作?

编辑:这是我正在使用的代码及其工作原理,但不确定我是否正确实现...

这是安排 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);
}

这是我的 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());
}

最佳答案

WorkManager 不支持周期性(即重复)内容 URI 触发器,但它支持使用 addContentUriTrigger()OneTimeWorkRequest 入队。作为约束条件之一。

public static void scheduleWork(String tag) {
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 photoCheckWork = photoCheckBuilder.build();
WorkManager instance = WorkManager.getInstance();
instance.enqueueUniqueWork(tag, ExistingPeriodicWorkPolicy.REPLACE, photoCheckWork);
}

因为它是一个 OneTimeWorkRequest,它只会在 URI 第一次更改时触发 - 当您的 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

// Then start listening for more changes
scheduleWork(getTags().iterator().next());
}
}

关于android - 如何使用 Workmanager 检测新照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53581969/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com