Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring File Access on Android

Tags:

android

I need to monitor files that are accessed (or tried to access and failed) by a specific program on my Android device. Something like FileMon on PCs.

There are suggestions on the Net to decompile the program, but I'm looking for a more straight way.

Thanks.

like image 615
Delphi.Boy Avatar asked Mar 25 '26 04:03

Delphi.Boy


1 Answers

You should use standard android.os.FileObserver, this Monitors files (using inotify) to fire an event after files are accessed or changed by by any process on the device (including this one). FileObserver is an abstract class; subclasses must implement the event handler onEvent(int, String). Refer to this link http://developer.android.com/reference/android/os/FileObserver.html

Simple example here:

import android.os.FileObserver;   
import android.util.Log;   


    /**  
     * SD Card file monitor  
     * 
     */  
public class SDCardListener extends FileObserver {   

       public SDCardListener(String path) { 
              super(path);   
       }   

       @Override  
       public void onEvent(int event, String path) {          
              switch(event) {   
                     case FileObserver.ALL_EVENTS:   
                            Log.d("all", "path:"+ path);   
                            break;   
                     case FileObserver.CREATE:   
                            Log.d("Create", "path:"+ path);   
                            break;   
              }   
      }   
}

SDCardListener listener = new SDCardListener("YOURDIRPATH");   

//Start monitoring   
listener.startWatching();

// You can try to access the directory here 

//Stop monitoring   
listener.stopWatching();  

One thing you should bare in mind is that you should keep a valid reference to the file observer object to prevent it from GC.

like image 106
Robin Avatar answered Mar 26 '26 18:03

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!