I defined FileObserver class outside the activity
public class MyFileObserver extends FileObserver {
public String absolutePath;
public MyFileObserver(String path) {
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
}
@Override
public void onEvent(int event, String path) {
if (path == null) {
return;
}
if ((FileObserver.CREATE & event)!=0) {
TextView tv = (TextView)findViewById(R.id.text1);
//Set the text
tv.setText("CREATED!");
}
}
}
and created an instance and started watching the file using StartWatching() in main activity
MyFileObserver fileOb = new MyFileObserver("/mnt/sdcard/");
fileOb.startWatching();
when i run the application and copy file in the directory being observed OnEvent() does not gets triggered, but when I click the button (used for other purpose defined in main activity) OnEvent triggers and gives the desired result. My question is that why this OnEvent() function does not trigger itself ? Is it because I am calling startWatching() in mainactivity, if this is the case then where should I call this function. Thanks in advance.
here is my onCreate() method .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFileObserver fileOb = new MyFileObserver("/mnt/sdcard/");
fileOb.startWatching();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}