版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/77932391
一. 概述:
android.os包下的FileObserver类是一个用于监听文件访问、创建、修改、删除、移动等操作的监听器,基于linux的INotify。FileObserver是个抽象
类,必须继承它才能使用。每个FileObserver对象监听一个单独的文件或者文件夹,如果监视的是一个文件夹,那么文件夹下所有的文件和级联子目录的改
变都会触发监听的事件。
二. 监听的事件类型:
ACCESS,即文件被访问
MODIFY,文件被 修改
ATTRIB,文件属性被修改,如 chmod、chown、touch 等
CLOSE_WRITE,可写文件被 close
CLOSE_NOWRITE,不可写文件被 close
OPEN,文件被 open
MOVED_FROM,文件被移走,如 mv
MOVED_TO,文件被移来,如 mv、cp
CREATE,创建新文件
DELETE,文件被删除,如 rm
DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
ALL_EVENTS,包括上面的所有事件
三.实例
import com.example.androidemail.R;
import com.example.androidemail.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
public class AndroidFileListenerActivity extends Activity {
private FileObserver mFileObserver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(null == mFileObserver) {
mFileObserver = new SDCardFileObserver(Environment.getExternalStorageDirectory().getPath());
mFileObserver.startWatching(); //开始监听
}
}
public void onDestory() {
if(null != mFileObserver) mFileObserver.stopWatching(); //停止监听
}
static class SDCardFileObserver extends FileObserver {
//mask:指定要监听的事件类型,默认为FileObserver.ALL_EVENTS
public SDCardFileObserver(String path, int mask) {
super(path, mask);
}
public SDCardFileObserver(String path) {
super(path);
}
@Override
public void onEvent(int event, String path) {
final int action = event & FileObserver.ALL_EVENTS;
switch (action) {
case FileObserver.ACCESS:
System.out.println("event: 文件或目录被访问, path: " + path);
break;
case FileObserver.DELETE:
System.out.println("event: 文件或目录被删除, path: " + path);
break;
case FileObserver.OPEN:
System.out.println("event: 文件或目录被打开, path: " + path);
break;
case FileObserver.MODIFY:
System.out.println("event: 文件或目录被修改, path: " + path);
break;
case FileObserver.CREATE:
System.out.println("event: 文件或目录被创建, path: " + path);
break;
}
}
}
}
onEvent是回调,系统监听到事件后会触发此事件,参数event就是上面所说的事件类型,参数path就是触发事件的目录,鉴定只针对于该层目录,其他层次无效。
我们大多数需要监听path目录下的所有文件对象的相关操作,那该如何是好呢?解决问题方法之一就是重新实现FileObserver类,
下面是对FileObserver类的重写实现过程
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import android.os.FileObserver;
import android.util.Log;
@SuppressWarnings(value = { "rawtypes", "unchecked" })
public class MultiFileObserver extends FileObserver {
/** Only modification events */
public static int CHANGES_ONLY = CREATE | MODIFY |DELETE | CLOSE_WRITE
| DELETE_SELF | MOVE_SELF | MOVED_FROM | MOVED_TO;
private List<SingleFileObserver> mObservers;
private String mPath;
private int mMask;
public MuityFileObserver(String path) {
this(path, ALL_EVENTS);
}
public MuityFileObserver(String path, int mask) {
super(path, mask);
mPath = path;
mMask = mask;
}
@Override
public void startWatching() {
if (mObservers != null)
return;
mObservers = new ArrayList<SingleFileObserver>();
Stack<String> stack = new Stack<String>();
stack.push(mPath);
while (!stack.isEmpty()) {
String parent = stack.pop();
mObservers.add(new SingleFileObserver(parent, mMask));
File path = new File(parent);
File[] files = path.listFiles();
if (null == files)
continue;
for (File f : files) {
if (f.isDirectory() && !f.getName().equals(".")
&& !f.getName().equals("..")) {
stack.push(f.getPath());
}
}
}
for (int i = 0; i < mObservers.size(); i++) {
SingleFileObserver sfo = mObservers.get(i);
sfo.startWatching();
}
};
@Override
public void stopWatching() {
if (mObservers == null)
return;
for (int i = 0; i < mObservers.size(); i++) {
SingleFileObserver sfo = mObservers.get(i);
sfo.stopWatching();
}
mObservers.clear();
mObservers = null;
};
@Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.ACCESS:
Log.i("RecursiveFileObserver", "ACCESS: " + path);
break;
case FileObserver.ATTRIB:
Log.i("RecursiveFileObserver", "ATTRIB: " + path);
break;
case FileObserver.CLOSE_NOWRITE:
Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path);
break;
case FileObserver.CLOSE_WRITE:
Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path);
break;
case FileObserver.CREATE:
Log.i("RecursiveFileObserver", "CREATE: " + path);
break;
case FileObserver.DELETE:
Log.i("RecursiveFileObserver", "DELETE: " + path);
break;
case FileObserver.DELETE_SELF:
Log.i("RecursiveFileObserver", "DELETE_SELF: " + path);
break;
case FileObserver.MODIFY:
Log.i("RecursiveFileObserver", "MODIFY: " + path);
break;
case FileObserver.MOVE_SELF:
Log.i("RecursiveFileObserver", "MOVE_SELF: " + path);
break;
case FileObserver.MOVED_FROM:
Log.i("RecursiveFileObserver", "MOVED_FROM: " + path);
break;
case FileObserver.MOVED_TO:
Log.i("RecursiveFileObserver", "MOVED_TO: " + path);
break;
case FileObserver.OPEN:
Log.i("RecursiveFileObserver", "OPEN: " + path);
break;
default:
Log.i("RecursiveFileObserver", "DEFAULT(" + event + " : " + path);
break;
}
}
/**
* Monitor single directory and dispatch all events to its parent, with full
* path.
*/
class SingleFileObserver extends FileObserver {
String mPath;
public SingleFileObserver(String path) {
this(path, ALL_EVENTS);
mPath = path;
}
public SingleFileObserver(String path, int mask) {
super(path, mask);
mPath = path;
}
@Override
public void onEvent(int event, String path) {
String newPath = mPath + "/" + path;
MultiFileObserver .this.onEvent(event, newPath);
}
}
}