2

I want to be notified when a specific file changes. I was looking into FileObserver but after having read the answer in this thread, I've tried to accomplish my target with a JobScheduler that should run the job when the file changes, using addTriggerContentUri().

I started by copying the example from the documentation page, changing the Uri that should trigger the job to Uri.fromFile(File(<path-to-file>)). Unfortunately, my onStartJob() method is never called although JobScheduler::schedule() returns success.

Now, I am wondering if I've done anything wrong or if there is a general flaw in my interpretations, maybe it is just impossible to trigger a job on ordinary file changes?

The stripped-down version of my class/code looks like this (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
    }
}

Of course, FilechangeService::scheduleJob() is called from an other part of my application. Furthermore, I have added the Service to my AndroidManifest.xml with android:permission="android.permission.BIND_JOB_SERVICE".

Thanks in advance for any help!

Binabik
  • 1,013
  • 11
  • 24
  • 1
    `addTriggerContentUri` works only with "content" `Uri`s - those that start with `content://` – pskink Sep 17 '19 at 09:46
  • I feared something like that. Is it possible to convert a File into such a "content" ``Uri``? – Binabik Sep 17 '19 at 14:23
  • no, there is no such way, I don't know why you don't want to use `FileObserver`? – pskink Sep 17 '19 at 14:26
  • The thread mentioned at the beginning of my question says that ``FileObserver`` is not reliable because Android could stop that process at any time. – Binabik Sep 17 '19 at 14:33
  • so use a [service](https://developer.android.com/guide/components/services), foreground service – pskink Sep 17 '19 at 14:38
  • I dislike the mandatory notification that has to be shown to the user when using a foreground service (just for observing a single file), but it seems to me that there is just no other way to achieve what I want ... – Binabik Sep 17 '19 at 16:05
  • yes, you have to live with that ;-) – pskink Sep 17 '19 at 16:12

1 Answers1

0

To sum up what has been said in the comments, it seems to be impossible to do what I tried to achieve. This is due to the fact that the Uri has to be a "content Uri", so a "file Uri" seems to be invalid.

Binabik
  • 1,013
  • 11
  • 24