为文件更改通知安排作业
Schedule Job for file change notifications
我想在特定文件更改时收到通知。我正在调查 FileObserver
,但在阅读 , I've tried to accomplish my target with a JobScheduler
that should run the job when the file changes, using addTriggerContentUri()
中的答案后。
我首先从文档页面复制示例,将应该触发作业的 Uri
更改为 Uri.fromFile(File(<path-to-file>))
。不幸的是,尽管 JobScheduler::schedule()
returns 成功,但从未调用我的 onStartJob()
方法。
现在,我想知道我是否做错了什么,或者我的解释是否存在一般缺陷,也许只是不可能在普通文件更改时触发作业?
我的 class/code 的精简版看起来像这样 (Kotlin):
class FilechangeService : JobService() {
companion object {
private const val JOB_ID = 0
// Schedule this job, replace any existing one.
fun scheduleJob(context: Context) {
// JobInfo Builder to construct the Job
val builder = JobInfo.Builder(
JOB_ID,
ComponentName(context, FilechangeService::class.java)
)
// Look for changes to specific file
val uri = Uri.fromFile(File(<path-to-file>))
builder.addTriggerContentUri(
JobInfo.TriggerContentUri(uri, 0)
)
val jobInfo = builder.build()
val js = context.getSystemService(JobScheduler::class.java)
val status = js.schedule(jobInfo)
}
}
override fun onStartJob(params : JobParameters) : Boolean {
// Code that is never executed =(
return false
}
}
当然,FilechangeService::scheduleJob()
是从我的应用程序的其他部分调用的。此外,我已使用 android:permission="android.permission.BIND_JOB_SERVICE"
.
将服务添加到我的 AndroidManifest.xml
在此先感谢您的帮助!
总结一下评论中所说的,似乎无法实现我试图实现的目标。这是因为 Uri
必须是 "content Uri",所以 "file Uri" 似乎无效。
我想在特定文件更改时收到通知。我正在调查 FileObserver
,但在阅读 JobScheduler
that should run the job when the file changes, using addTriggerContentUri()
中的答案后。
我首先从文档页面复制示例,将应该触发作业的 Uri
更改为 Uri.fromFile(File(<path-to-file>))
。不幸的是,尽管 JobScheduler::schedule()
returns 成功,但从未调用我的 onStartJob()
方法。
现在,我想知道我是否做错了什么,或者我的解释是否存在一般缺陷,也许只是不可能在普通文件更改时触发作业?
我的 class/code 的精简版看起来像这样 (Kotlin):
class FilechangeService : JobService() {
companion object {
private const val JOB_ID = 0
// Schedule this job, replace any existing one.
fun scheduleJob(context: Context) {
// JobInfo Builder to construct the Job
val builder = JobInfo.Builder(
JOB_ID,
ComponentName(context, FilechangeService::class.java)
)
// Look for changes to specific file
val uri = Uri.fromFile(File(<path-to-file>))
builder.addTriggerContentUri(
JobInfo.TriggerContentUri(uri, 0)
)
val jobInfo = builder.build()
val js = context.getSystemService(JobScheduler::class.java)
val status = js.schedule(jobInfo)
}
}
override fun onStartJob(params : JobParameters) : Boolean {
// Code that is never executed =(
return false
}
}
当然,FilechangeService::scheduleJob()
是从我的应用程序的其他部分调用的。此外,我已使用 android:permission="android.permission.BIND_JOB_SERVICE"
.
在此先感谢您的帮助!
总结一下评论中所说的,似乎无法实现我试图实现的目标。这是因为 Uri
必须是 "content Uri",所以 "file Uri" 似乎无效。