Register a BroadcastReceiver for listing to changes in a folder
<p>I try to update a <code>TextView</code> dynamically based on number of files in a folder. I read that I can do this by register a BroadCastReciever and a Service. This is what I've done so far:</p>

<p>I've created a class that listens for changes in a folder:</p>

<pre><code>package Logic;

public class FileObserverForNewFirFiles extends FileObserver {

String path;
int counter;

public FileObserverForNewFirFiles(String path) {
    super(path, FileObserver.ALL_EVENTS);
    this.path = path; 
}

@Override
public void onEvent(int event, String path) {

    if(path == null)
        return; 

    if((FileObserver.CREATE &amp; event) != 0)  {
        counter++;
    }
    if((FileObserver.DELETE &amp; event) != 0) {
        counter--; 
    }   
}

public int getFileCount() {
    return counter; 
}
}
</code></pre>

<p>And a class that extends service</p>

<pre><code>public class FileObserverService extends Service {

private FileObserverForNewFirFiles fObs;
private final Handler handler = new Handler();
Intent intent;
public static final String BROADCAST_ACTION = "My.Class.Activty"; 
int i;
File f;

@Override
public void onCreate() {
    super.onCreate();
    f = new File("/mnt/sdcard/Bluetooth"); 
    fObs = new FileObserverForNewFirFiles(f.getAbsolutePath()); 
    intent = new Intent(BROADCAST_ACTION);  
}

@Override
public void onStart(Intent i, int startid) {
    fObs.startWatching();
    handler.removeCallbacks(sendUpdatesToUI);
    handler.postDelayed(sendUpdatesToUI, 1000); // 1 second

}

private Runnable sendUpdatesToUI = new Runnable() {
    public void run() {
        createFileObserver(f);
        handler.postDelayed(this, 10000); // 10 seconds
    }
};    

public void createFileObserver(File f) {

    if(!f.isDirectory()) {
        Log.i("FILEOBSERVER: ", "FILE NOT A DIRECTORY!"); 
    }
    else {
        i = fObs.getFileCount();
        intent.putExtra("files", i); 
    }
    sendBroadcast(intent);
}


@Override 
public void onDestroy() {
    fObs.stopWatching();
    handler.removeCallbacks(sendUpdatesToUI);       
    super.onDestroy();
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}
</code></pre>

<p>From the <code>onCreate()</code> method Activity where I want to update the <code>TextView</code>: </p>

<pre><code>serviceIntent = new Intent(this, FileObserverService.class); 
</code></pre>

<p>and then register a <code>BroadcastReceiver</code>:</p>

<pre><code>private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setOverView(intent);       
        }
    };    

    @Override
    public void onResume() {
        super.onResume();       
        startService(serviceIntent);
        registerReceiver(broadcastReceiver, new IntentFilter(FileObserverService.BROADCAST_ACTION));
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
        stopService(serviceIntent);         
    }   
</code></pre>

<p>And finally, the method for setting the content of the <code>TextView</code></p>

<pre><code>public void setOverView(Intent i) {
    numberOfCasesInFolder = gci.getCasesFromFolder();
    int databaseCount = db.countCases();
    sent.setText("Sent: " + databaseCount + " cases");



    int numberOfFiles = i.getIntExtra("files", 0);

    ready.setText(Integer.toString(numberOfFiles)); 

    /*ready.setText("Ready: " + Integer.toString(numberOfCasesInFolder)
            + " cases");*/
}
</code></pre>

<p>But the <code>TextView</code> doesn't seems to receive anything, and the default value, 0, is added. Can someone point me in the right direction? </p>

<p>Thanks in advance! :)</p>

<p><strong>EDIT</strong></p>

<p>After some debugging, I found out that this method</p>

<pre><code>public int getFileCount() {
    return counter; 
}
</code></pre>

<p>always return 0, but I can't see how that is possible.</p>
