P.C.
P.C.

Reputation: 93

Alternative to FileObserver via BroadcastReceiver?

I need to monitor files placed in SD card (recursively). As Broadcast receiver can receive system events by setting specific intent filter "action", we can probably use it to handle file change events like Open, Delete, etc. All we need to know are what "actions", "category" & "data" values are to be set in manifest file along with any specific "user-permissions" to be used.

The other way is to implement FileObserver but this has issues with garbage collection, even if we create a Service to hold its object the Service itself may be terminated when low on resources(Point 5.2 http://www.vogella.com/articles/AndroidServices/article.html) so its not exactly a full proof method.

Can you please let me know which configuration values will enable BroadcastReceiver to receive such events/intents?

Upvotes: 3

Views: 1757

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006869

As Broadcast receiver can receive system events by setting specific intent filter "action", we can probably use it to handle file change events like Open, Delete, etc.

No. Ordinary Java I/O operations do not result in broadcasts.

The other way is to implement FileObserver but this has issues with garbage collection, even if we create a Service to hold its object the Service itself may be terminated when low on resources... so its not exactly a full proof method.

There is no "foolproof" method for you to continuously monitor external storage operations, other than by modifying the firmware.

I would use AlarmManager to periodically scan the directory for changes, with a polling period chosen by the user, so they can control battery and CPU consumption by your app.

Upvotes: 3

Related Questions