Here's a new library for you all: FileObserver.
Documentation for the native Android FileObserver class can be found here: FileObserver | Android Developers.
Description:
Monitors files and fires events after files are accessed or changed by any process on the device.
Each FileObserver instance monitors a single file or directory.
If a directory is monitored, events will be triggered for all files and subdirectories (recursively) inside the monitored directory.
That's the official description, i found a page here where the author states:
So that's something to watch for.
The library has just 5 methods:
EventTypeToString (EventType As Int) As String
Pass an EventType Int to this method to retrieve the EventType's String identifier.
Initialize (EventName As String, Path As String)
Initialize the FileObserver with an EventName and Path.
All FileObserver EventTypes will be triggered.
Initialize2 (EventName As String, Path As String, EventMask As Int)
Initialize the FileObserver with an EventName, Path and EventMask.
The EventMask will filter the EventTypes that will be triggered.
StartWatching
Start watching for events.
StopWatching
Stop watching for events.
FileObserver generates a single event:
OnEvent (EventType As Int, Path As String)
EventType is an Int which identifies the type of event, check the official documentation for possible EventTypes.
Path is the folder or file that the event applies to.
Path will be a partial path, it will not be the complete path to the folder or directory.
You will need to keep a reference to the Path value used in Initialize or Initialize2 and use that along with the event's Path value to create a full path.
If you call Initialize or Initialize2 with an EventName and the event handler Sub does not exist then the FileObserver will NOT be initialized.
Here's some example code to test the library:
Testing with an Activity is tricky...
You need to trigger an event in the monitored folder without stopping the FileObserver from listening.
I found that running this Activity then pressing the Home key followed by using a file explorer to navigate to my SD card's media folder enabled me to trigger an event.
Version 1.00 is attached along with the example code.
Martin.
Documentation for the native Android FileObserver class can be found here: FileObserver | Android Developers.
Description:
Monitors files and fires events after files are accessed or changed by any process on the device.
Each FileObserver instance monitors a single file or directory.
If a directory is monitored, events will be triggered for all files and subdirectories (recursively) inside the monitored directory.
That's the official description, i found a page here where the author states:
One thing worth-mentioning is that the API documentation says “If a directory is monitored, events will be triggered for all files and subdirectories (recursively) inside the monitored directory”, but the FileObserver API is actually not recursive.
So that's something to watch for.
The library has just 5 methods:
EventTypeToString (EventType As Int) As String
Pass an EventType Int to this method to retrieve the EventType's String identifier.
Initialize (EventName As String, Path As String)
Initialize the FileObserver with an EventName and Path.
All FileObserver EventTypes will be triggered.
Initialize2 (EventName As String, Path As String, EventMask As Int)
Initialize the FileObserver with an EventName, Path and EventMask.
The EventMask will filter the EventTypes that will be triggered.
StartWatching
Start watching for events.
StopWatching
Stop watching for events.
FileObserver generates a single event:
OnEvent (EventType As Int, Path As String)
EventType is an Int which identifies the type of event, check the official documentation for possible EventTypes.
Path is the folder or file that the event applies to.
Path will be a partial path, it will not be the complete path to the folder or directory.
You will need to keep a reference to the Path value used in Initialize or Initialize2 and use that along with the event's Path value to create a full path.
If you call Initialize or Initialize2 with an EventName and the event handler Sub does not exist then the FileObserver will NOT be initialized.
Here's some example code to test the library:
B4X:
' FileObserverDemo Activity module
Sub Process_Globals
End Sub
Sub Globals
Dim FileObserver1 As FileObserver
End Sub
Sub Activity_Create(FirstTime As Boolean)
FileObserver1.Initialize("FileObserver1", File.DirRootExternal&"/media")
' example of Initialize2 syntax
' FileObserver1.Initialize2("FileObserver1", File.DirRootExternal&"/media", FileObserver1.OPEN)
FileObserver1.StartWatching
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
' FileObserver1.StopWatching
End Sub
Sub FileObserver1_OnEvent (EventType As Int, Path As String)
Log("EventType="&FileObserver1.EventTypeToString(EventType)&", Path="&Path)
Select EventType
Case FileObserver1.ACCESS
' handle event
Case FileObserver1.ATTRIB
' handle event
Case FileObserver1.CLOSE_NOWRITE
' handle event
Case FileObserver1.CLOSE_WRITE
' handle event
Case FileObserver1.CREATE
' handle event
Case FileObserver1.DELETE
' handle event
Case FileObserver1.DELETE_SELF
' handle event
Case FileObserver1.MODIFY
' handle event
Case FileObserver1.MOVE_SELF
' handle event
Case FileObserver1.MOVED_FROM
' handle event
Case FileObserver1.MOVED_TO
' handle event
Case FileObserver1.OPEN
' handle event
End Select
End Sub
Testing with an Activity is tricky...
You need to trigger an event in the monitored folder without stopping the FileObserver from listening.
I found that running this Activity then pressing the Home key followed by using a file explorer to navigate to my SD card's media folder enabled me to trigger an event.
Version 1.00 is attached along with the example code.
Martin.