I would like to receive an event whenever a file was added to a specific folder, e.g. the download folder. To reach this I tried 3 different approaches without any success. The target devices are Android 15+. Do you have any experience with any of these 3 approaches and could help out with a working sample?
APPROACH 1 - FileObserver:
In a background service I add a recursive file observer for the top folder as described here. On Android 4/5 its working but on Android 6 no events are fired (known issue) On top of that it seems that on Android 4/5 the file observer is not reliable. At some point the stopWatching() method gets called and from then on no event will be received.
in onStartCommand(..) of the service:
new MyFileObserver(Constants.DOWNLOAD_PATH, true).startWatching();
APPROACH 2 - Content Observer:
I tried to tweak the content observer for my use case (as described here), but I never receive any events.
in onStart of the service:
getContentResolver().registerContentObserver( Uri.parse("content://download/"), true,
new MyObserver(myHandler));
.
public class MyObserver extends ContentObserver {
// left blank below constructor for this Contact observer example to work
// or if you want to make this work using Handler then change below registering //line
public MyObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
Log.e("", "~~~~~~ on change" + selfChange);
// Override this method to listen to any changes
}
@Override
public void onChange(boolean selfChange, Uri uri) {
// depending on the handler you might be on the UI
// thread, so be cautious!
Log.e("", "~~~~~~ on change uri" + selfChange);
}
}
APPROACH 3 - BroadcastReceiver:
With a BroadcastReceiver I try to get the ON_DOWNLOAD_COMPLETE_EVENT (as described here. But nothing happens.
in on StartCommand(...) of the service:
registerReceiver(new DownloadListenerService(), new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
DownloadListenerService:
public class DownloadListenerService extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
System.out.println("got here");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
editor.putString("downloadPath", downloadPath);
editor.commit();
}
}
}
Manifest:
<receiver
android:name=".DownloadListenerService"
android:icon="@mipmap/ic_launcher"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
Your 3rd approach is the best one and the most efficient one. However try using the full package name in your receiver tag in the Manifest. The problem is that the action is sent to a receiver that is not found. For example:
Actually, right now there is no known work-around for Android 6.0 and above. In Android 4/5/5.1 the
FileObserver
is working fine mostly, but for Android 6 you simply can't get any kind of response from the system when a file is added to an external directory. Knowing thisFileObserver
is completely useless in Android 6.But eventually you can detect added content in the Android system, with the Content Observer which is also working fine in Android 6. Maybe this can solve your problem until Google provides a fix.
This is how I currently use the ContenObserver:
An than the MyContentOberserver.class I just check for last edited files in my specific path and if they are not older than 5-10 seconds I assume that this triggered the ContentObserver Event.
UPDATE EDIT:
This is how it should work for you:
In your BackgroundService.class:
And than inside the MyContentObserver.class:
I hope this helps, let me now If it works for you. It work's for me with the download folder on android 6.1 :)
I have tried your APPROACH 3-BroadcastReceiver in my android Application and it works for me. Please go through the code it may help you.You can change the image url according to your requirements.
My Activity and Broadcast receiver class:
My Pojo class:
My Manifest Declaration: