2012-06-22 59 views
5

,我發現了一個偉大類擴展抽象文件觀察類的工作...文件觀察員不是從意圖服務

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函數不起作用。

任何想法?謝謝!

+1

不要硬編碼sdcard的路徑,從Context.getExternalStorageDirectory()獲取它。如果您打算在SD卡上創建任何內容,請確保您具有寫入外部存儲清單權限。 –

回答