且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

接收事件,如果文件已下载/已添加到下载文件夹

更新时间:2023-01-05 14:12:02

实际上,目前没有已知的解决方法适用于Android 6.0及更高版本。在Android 4/5 / 5.1中, FileObserver 基本上可以正常工作,但是对于Android 6,当将文件添加到Android 6时,您根本无法从系统获得任何响应外部目录。知道这个 FileObserver 在Android 6中是完全没有用的。

Actually, right now there is no known work-around for Android 6.0 and above. In Android 4/5/5.1 the FileObserver is working fine mostly, but for Android 6 you simply can't get any kind of response from the system when a file is added to an external directory. Knowing this FileObserver is completely useless in Android 6.

但是最终,您可以使用Content Observer来检测Android系统中添加的内容。在Android 6中也可以正常工作。也许这可以解决您的问题,直到Google提供修复程序为止。

But eventually you can detect added content in the Android system, with the Content Observer which is also working fine in Android 6. Maybe this can solve your problem until Google provides a fix.

这是我目前使用ContenObserver的方式:

This is how I currently use the ContenObserver:

mycontentobserver = new MyContentObserver(handler,**Your path**,this);
getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, mycontentobserver);

比MyContentOberserver.class好,我只是检查特定路径中最后编辑的文件,然后如果它们不早于5到10秒,我认为这触发了ContentObserver事件。

An than the MyContentOberserver.class I just check for last edited files in my specific path and if they are not older than 5-10 seconds I assume that this triggered the ContentObserver Event.

这是为您工作的方式:

在您的 BackgroundService.class 中:

mycontentobserver = new MyContentObserver(handler,**Your download folder path**,this);
getContentResolver().registerContentObserver(MediaStore.Files.getContentUri("external"), true, mycontentobserver);

而不是在 MyContentObserver.class 内部:

public MyContentObserver(Handler handler, String workpath,  ContentModificationService workcontext) {
    super(handler);
    downloadfolderpath = workpath;
    contentcontext = workcontext;
}

@Override
public void onChange(boolean selfChange, Uri uri) {

   if(downloadfolderpath != null) {
      File file = new File(downloadfolder);
      if (file.isDirectory()) {
         listFile = file.listFiles();
         if (listFile != null && listFile.length > 0) {
            
            // Sort files from newest to oldest (this is not the best Method to do it, but a quick on)
            Arrays.sort(listFile, new Comparator<File>() {
               public int compare(File f1, File f2) {
               return      Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
               }
            });

            if (listFile[listFile.length - 1].lastModified() >= System.currentTimeMillis() - 5000) { //adjust the time (5000 = 5 seconds) if you want.

               //Some file was added !! Yeah!
               //Use the contentcontext to launch some Method in you Service
               //For example:
               contentcontext.SomeMethodToContinue();
            }
         }
      }
   }
}

希望对您有所帮助,请让我现在使用。它适用于android 6.1上的下载文件夹:)

I hope this helps, let me now If it works for you. It work's for me with the download folder on android 6.1 :)