android - 如何获得从 SD 卡中删除任何文件的通知

标签 android android-6.0-marshmallow fileobserver

我要创建 Dumpster像应用程序一样,为此我希望在用户删除任何文件时收到通知,以便我可以将其保存到我的应用程序内存中。

我使用了 File Observer,但它会在文件删除后发出通知,而在 marshmallow 中,它也不会通知删除。 我提到了this link对于文件观察员。 我在某处读到可以使用 native 编程语言 (C),但无法获得任何解决方案。我怎样才能做到这一点? 提前致谢。

我试过这个:

@Override
        public void onEvent(int event, String path) {
            if (path == null) {
                return;
            }
            //the monitored file or directory was deleted, monitoring effectively stops
            if ((FileObserver.DELETE_SELF & event)!=0) {
                FileAccessLogStatic.accessLogMsg += absolutePath + "/" + " is deleted\n";
            }       
        }

最佳答案

让我们先澄清一下。

  1. Dumpster 使用 .trash 目录,该目录可能始终存在也可能不存在。应该注意的是,通过谷歌评论可以看出 Dumpster 在许多设备上无法正常运行。
  2. Dumpster 使用(我从代码中猜测它仅用于教育目的)它自己的系统文件 Handler 使用 service 检查onClick 事件,如果它是一个文件 onClick,它会将 File 及其 path 保存到一个单独的文件夹中(通常是隐藏的)并将其保存在本地的 database 中。如果它被删除,你知道文件在哪里,如果没有,让我们从那个隐藏文件夹中删除那个文件。好吧,这有点不值得,因为您几乎需要让您的服务几乎所有时间都在使用 CPU 资源。它也在 Root设备上运行,但为什么根设备仅用于此目的。
  3. 随着设备安全性的提高,执行这些任务的可能性越来越小。作为最新的 1-09-2017 所有这些文件回收站 对最新的 android 版本都有负面评论。因此,证明我的观点。

    FileObserver 使用检查文件甚至目录的任何更改的概念,但您无法影响它,这意味着您无法阻止删除,它会在用户删除后通知所有内容.

    inotify.h 它用于 NDK 创建应用程序,用于检查文件夹和文件上的事件,但如果文件夹提到子子文件夹将不会在此涵盖或通知您文件的任何更改。此外,inotify 中使用的概念与 FileObserver 相同。您只能在文件被删除后收到通知。 inotify中使用的代码是这样的。

  1. Create the inotify instance by inotify_init().
  2. Add all the directories to be monitored to the inotify list using inotify_add_watch() function.
  3. To determine the events occurred, do the read() on the inotify instance. This read will get blocked till the change event occurs. It is recommended to perform selective read on this inotify instance using select() call.
  4. Read returns list of events occurred on the monitored directories. Based on the return value of read(), we will know exactly what kind of changes occurred.
  5. In case of removing the watch on directories / files, call inotify_rm_watch().

inotify 中的两个方法如下:

IN_DELETE – 从监视目录中删除的文件/目录

IN_DELETE_SELF – 监视的文件/目录本身已被删除

两者都与 FileObserver 中的几乎相同

  1. 此解决方案可能无法完全提供帮助,但仍可帮助创建任何Dumpster 类型的应用程序。 可以说,您需要创建自己的 File Manager,您可以在其中创建自己的自定义 FileV2(只是一个很酷的名称 File version 2.0)扩展 的类File 并且您可以根据需要覆盖 delete 方法(以及所有其他方法)。您可以创建一个自定义弹出,说您是否要删除您自己的备份文件在 yes 上 dismissing 在 no 上弹出。 (确保用户使用此文件管理器进行删除,否则它将无法工作,因为覆盖系统文件删除()只会弄乱其他应用程序)。

    class filev2 extends File {
    
    public filev2(@NonNull String pathname) {
        super(pathname);
    }
    
    public filev2(@NonNull URI uri) {
        super(uri);
    }
    
    @Override
    public boolean delete() {
     //   return super.delete();
    //Do as you want and return the boolean.
    }
    
    }
    

但是如果用户为此使用您的文件管理器,请确保您的文件将被保存。 您可以为任务设置 intent-filters,这样您的 FileManager 就可以进入 ACTION_VIEW

最后但我不确定是否也可以使用 registerContentObserver。 (虽然不确定)

来源:

Inotify.h help website

registerContentObserver help

Kind of Similar Question

FileObserver Help

Linux Help Deleted Log for Files

我希望它对您有所帮助,我希望您现在可以开始做您想要的事情。

关于android - 如何获得从 SD 卡中删除任何文件的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44720696/

相关文章:

java - 应用程序仅在发布版本中崩溃

java - android fileObserver onEvent 未被调用

android - FileObserver 在后台监听,Android O

java - Android FileObserver onEvent 没有被调用

java - 从类中读取变量

android - 如何让用户访问我的应用程序的内部存储目录?

安卓 "No content provider found for permission revoke"

android - setExactAndAllowWhileIdle - 与开发人员引用不完全一致

android - 为什么我的应用程序下载的文件在卸载后会被删除?

Android:具有不同项目的 ListView (优化)