Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – File Observer无法使用intent服务大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

发现了一个很好的类来扩展抽象的File Observer类……

import android.os.FiLeobserver;
import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.httpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import android.util.Log;

public class FileSync extends FiLeobserver {
public String absolutePath;
public String uid;

public FileSync(String path,String uidd) {
    super(path,FiLeobserver.all_EVENTS);
    absolutePath = path;
    uid = uidd;
}
@Override
public void onEvent(int event,String path) {
    if (path == null) { //path is the name of the file... I think its absolute
        return;
    }
    //a new file or subdirectory was created under the monitored directory
    if ((FiLeobserver.CREATE & event)!=0) { 
        doFileUpload(path,uid);
    }
    //a file or directory was opened
    if ((FiLeobserver.oPEN & event)!=0) {
        //TODO Nothing... yet
    }
    //data was read from a file
    if ((FiLeobserver.ACCESS & event)!=0) {
        //TODO Nothing... yet
    }
    //data was written to a file
    if ((FiLeobserver.MODIFY & event)!=0) {
        doFileUpload(path,uid);
    }
    //someone has a file or directory open read-only,and closed it
    if ((FiLeobserver.CLOSE_NowRITE & event)!=0) {
        //TODO Nothing... yet
    }
    //someone has a file or directory open for wriTing,and closed it 
    if ((FiLeobserver.CLOSE_WRITE & event)!=0) {
        doFileUpload(path,uid);
    }
    //[todo: consider combine this one with one below]
    //a file was deleted from the monitored directory
    if ((FiLeobserver.deletE & event)!=0) {
        //TODO Remove file from the server
    }
    //the monitored file or directory was deleted,monitoring effectively stops
    if ((FiLeobserver.deletE_SELF & event)!=0) {
        //TODO Toast an error,recreate the folder,resync and restart monitoring
    }
    //a file or subdirectory was moved from the monitored directory
    if ((FiLeobserver.MOVED_FROM & event)!=0) {
        //TODO delete from the server
    }
    //a file or subdirectory was moved to the monitored directory
    if ((FiLeobserver.MOVED_TO & event)!=0) {
        doFileUpload(path,uid);
    }
    //the monitored file or directory was moved; monitoring conTinues
    if ((FiLeobserver.MOVE_SELF & event)!=0) {
        //TODO Recreate the folder and show toast
    }
    //Metadata (permissions,owner,timestamp) was changed explicitly
    if ((FiLeobserver.ATTRIB & event)!=0) {
        //TODO Nothing... Yet
    }
}

我在Intentservice的onCreate中创建了三个这样的观察者,如下所示:

new File("/sdcard/Docs/").mkdir();
FileSync files = new FileSync("/sdcard/Docs/",uid);
FileSync pictures = new FileSync(Environment.DIRECTORY_PICTURES,uid);
FileSync music = new FileSync(Environment.DIRECTORY_MUSIC,uid);
files.startWatching();
pictures.startWatching();
music.startWatching();

不仅观察者不工作,而且mkdir功能不起作用.

有任何想法吗?谢谢!

最佳答案
检查您是否在清单文件中包含了android.permission.WRITE_EXTERNAL_STORAGE.
缺少此权限将导致mkdir()失败,这将导致FileObserver失败,因为受监视的文件或目录必须在startWatching时存在,否则将不报告任何事件.

大佬总结

以上是大佬教程为你收集整理的android – File Observer无法使用intent服务全部内容,希望文章能够帮你解决android – File Observer无法使用intent服务所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。