In my app I have to detect when a new file is added in a folder, but I read in this post that FileObserver can not be used for newest Android versions (>6.0) so I'm asking: how can I do it? With ContentObserver? If yes, is there any example from where I can see how to use it?
Asked
Active
Viewed 424 times
0
-
Could you explain it a little bit more? There might be a better solution for your problem. Why do you need to observe a file? – Ernest Zamelczyk Jul 24 '18 at 14:48
-
I need to show a notification when a new file is added in a determinated folder (for example, if I set the observing folder to download folder and I download a file, it will notify me about the presence of a new file in download folder) – Cri Didi Jul 24 '18 at 15:01
1 Answers
0
Android can terminate any process by time, user choice and/or free up RAM; therefore, something like FileObserver is unreliable.
You need three parts:
A Service were the magic goes. The activity (foreground app) which will launch the service (if not already active)
An activity: to launch the service
A BroadcastReceiver which will receive a signal when the service is killed so it can restart it.
public class CheckFolderServiceRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, CheckFolderService.class));
}
}

acarlstein
- 1,799
- 2
- 13
- 21