Here are the examples of the java api class android.os.FileObserver taken from open source projects.
1. PFileIO#observeFolder()
Project: Protocoder
File: PFileIO.java
File: PFileIO.java
@ProtoMethod(description = "Observer file changes in a folder", example = "") @ProtoMethodParam(params = { "path", "function(action, file" }) public void observeFolder(String path, final FileObserverCB callback) { fileObserver = new FileObserver(ProjectManager.getInstance().getCurrentProject().getStoragePath() + "/" + path, FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE) { @Override public void onEvent(int event, String file) { if ((FileObserver.CREATE & event) != 0) { callback.event("create", file); } else if ((FileObserver.DELETE & event) != 0) { callback.event("delete", file); } else if ((FileObserver.MODIFY & event) != 0) { callback.event("modify", file); } } }; fileObserver.startWatching(); }
2. MainActivity#onCreate()
Project: Protocoder
File: MainActivity.java
File: MainActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // Window w = getWindow(); // in Activity's onCreate() for instance // w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); // w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //} setContentView(R.layout.activity_main); setToolbar(); mProtocoder = Protocoder.getInstance(this); mProtocoder.init(); // Get and store the application context AppRunnerContext.get().init(getApplicationContext()); /* * Views */ if (savedInstanceState == null) { addFragments(); } else { mProtocoder.protoScripts.reinitScriptList(); } // Check when mContext file is changed in the protocoder dir fileObserver = new FileObserver(ProjectManager.FOLDER_USER_PROJECTS, FileObserver.CREATE | FileObserver.DELETE) { @Override public void onEvent(int event, String file) { if ((FileObserver.CREATE & event) != 0) { MLog.d(TAG, "File created [" + ProjectManager.FOLDER_USER_PROJECTS + "/" + file + "]"); // check if its mContext "create" and not equal to probe because // thats created every time camera is // launched } else if ((FileObserver.DELETE & event) != 0) { MLog.d(TAG, "File deleted [" + ProjectManager.FOLDER_USER_PROJECTS + "/" + file + "]"); } } }; connectivityChangeReceiver = new ConnectivityChangeReceiver(); }