android–File Observer无法使用intent服务

android–File Observer无法使用intent服务,第1张

概述我发现了一个很好的类来扩展抽象的File Observer类......import android.os.FileObserver; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInpu

我发现了一个很好的类来扩展抽象的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;}@OverrIDepublic 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服务所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1140019.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存