tags:

views:

216

answers:

1

Hi,

I want to listen to the changes occured in file system.I am using FileObserver.Here is my code:

Code:

class MyDirObserver extends FileObserver { String superPath; public MyDirObserver(String path) { super(path, ALL_EVENTS); this.superPath=path; }

       public void onEvent(int event, String path) 
       { 
               Log.e("onEvent of Directory", "=== onEvent ==="); 
               try{ 
         _Dump("dir", event, path,superPath); 
               } 
               catch(NullPointerException ex) 
               { 
                       Log.e("ERROR","I am getting error"); 
               } 
       } 
     } 


     private void _Dump(final String tag, int event, String path,String 

superPath) {

       Log.d(tag, "=== dump begin ==="); 
       Log.d(tag, "path=" + path); 
       Log.d(tag,"super path="+superPath); 
       Log.d(tag, "event list:"); 
       if ((event & FileObserver.OPEN) != 0) 
       { 
                         Log.d(tag, "  OPEN"); 
       } 
       if ((event & FileObserver.CLOSE_NOWRITE) != 0) 
       { 


                             Log.d(tag, "  CLOSE_NOWRITE"); 


       } 
       if ((event & FileObserver.CLOSE_WRITE) != 0) 
       { 


            Log.d(tag, "  CLOSE_WRITE"); 


                    Log.i("NEWFILEOBSERVER","File is Modified"); 
             if(path!=null) 
               { 
                                                    Log.d("---------FilePath",superPath+path); 
                               } 


       } 
       if ((event & FileObserver.CREATE) != 0) 
       { 
               isCreate=true; 
                    Log.i("NEWFILEOBSERVER","File is Created "); 
                     if(path!=null) 
                       { 
                            Log.d("---------FilePath",superPath+path); 
                                                       } 
                         Log.d(tag, "  CREATE"); 


       } 
       if ((event & FileObserver.DELETE) != 0) 
       { 
                    Log.i("NEWFILEOBSERVER","File is deleted"); 
                     if(path!=null) 
                       { 
                            Log.d("---------FilePath",superPath+path); 
                                                       } 
             //  startMyActivity("A new file is deleted thats="+superPath); 


               Log.d(tag, "  DELETE"); 


       } 
       if ((event & FileObserver.DELETE_SELF) != 0) 
       { 
               Log.d(tag, "  DELETE_SELF"); 
       } 
       if ((event & FileObserver.ACCESS) != 0) 
       { 
               Log.d(tag, "  ACCESS"); 
       } 
       if ((event & FileObserver.MODIFY) != 0) 
       { 
               if(!isModified) 
             isModified=true; 


               if(isModified && isOpen) 
                       isAgainModified=true; 
               Log.d(tag, "  MODIFY"); 
       } 
       if ((event & FileObserver.MOVED_FROM) != 0) 
       { 
               Log.d(tag, "  MOVED_FROM"); 
               if(path!=null) 
               { 
                                            Log.d("---------FilePath",superPath+path); 
                                       } 
       } 
       if ((event & FileObserver.MOVED_TO) != 0) 
       { 
               Log.d(tag, "  MOVED_TO"); 


               if(path!=null) 
               { 
                                            Log.d("---------FilePath",superPath+path); 
                                       } 
       } 
       if ((event & FileObserver.MOVE_SELF) != 0) 
       { 
         Log.d(tag, "  MOVE_SELF"); 
       } 
       if ((event & FileObserver.ATTRIB) != 0) 
       { 
               Log.d(tag, "  ATTRIB"); 
       } 
       Log.d(tag, "=== dump end ==="); 

}

it stops after some time.I dont get the exact time but doesnt work always though I call startWatching() in service in a loop which runs for all the folders of sdcard and calls startWatching() for each of them. It shows unpredictable behaviour and stops listening for some folders and runs perfectly for the others.

I hope you guys help me.I tried many ways but it doesnt work perfectly.Am I doing something wrong??? or there we have some other way to do this.......

Please help me........I have to get this done withing few days......

Any help is appreciated!!!!

Thanks in Advance Nemat

A: 

DAre you sure the Observers are still alive? What thread are they running in? Maybe they get destroyed by the garbage collcector?

cboese
this problem arises when system gets idle for some time else it works fine.BTW Observers are not alive at that time.They may be garbbage collected.
Nemat