我最近在网上搜了一段关于android监听文件夹的程序可是不知道哪里有错,请大神帮助看看。public class SDCardListener extends FileObserver {
private Context context; public SDCardListener(Context context, String path) {
// 这种构造方法是默认监听所有事件的,如果使用super(String,int)这种构造方法, 则int参数是要监听的事件类型.
super(path);
this.context = context;
init();
}
/**
* 注册监听器
*/
public void init() {
IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentfilter.addDataScheme("file");
ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
context.registerReceiver(scanSdReceiver, intentfilter);
} @Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.CREATE://监听创建
Log.d("Create", "path:" + path);
rescanSdcard();
break;
case FileObserver.DELETE://监听删除
Log.d("Delete", "path:" + path);
rescanSdcard();
break;
}
}
/**
* 刷新SD卡
*/
public void rescanSdcard() {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory().getAbsolutePath())));
}
}
private Context context; public SDCardListener(Context context, String path) {
// 这种构造方法是默认监听所有事件的,如果使用super(String,int)这种构造方法, 则int参数是要监听的事件类型.
super(path);
this.context = context;
init();
}
/**
* 注册监听器
*/
public void init() {
IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentfilter.addDataScheme("file");
ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
context.registerReceiver(scanSdReceiver, intentfilter);
} @Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.CREATE://监听创建
Log.d("Create", "path:" + path);
rescanSdcard();
break;
case FileObserver.DELETE://监听删除
Log.d("Delete", "path:" + path);
rescanSdcard();
break;
}
}
/**
* 刷新SD卡
*/
public void rescanSdcard() {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory().getAbsolutePath())));
}
}
解决方案 »
- android --- google map 问题
- android关于emoji问题
- 【求大神解救】Camera onPreviewFrame 函数里的data问题
- 如何禁止手机横屏?
- Settings源码 PreferenceScreen 跳转Activity问题
- 在listview填加文字图片功能
- 请教一个ApiDemos样例程序的问题
- [SDK Manager] 系统找不到指定的路径。
- 急!Android4.1-bootchart运行错误!求大牛!
- android的DDMS报错的问题,有什么解决方案!?
- android: Json数据解析出错
- [求助]关于ViewFlipper的一些疑问, 能不能加载切换Activity
这句话上面啊