我如何获得从SD卡删除任何文件的通知 [英] How can I get notification of any file being deleted from SD card

查看:124
本文介绍了我如何获得从SD卡删除任何文件的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建类似应用程序的 Dumpster ,为此,我想在用户删除任何文件时通知我,以便将其保存到我的应用程序内存中.

I want to create Dumpster like app, for this I want notification when user is deleting any file so that I can save it to my app memory.

我使用了File Observer,但是它在删除文件后发出通知,并且在棉花糖中它也不通知删除. 我为文件查看器引用了此链接. 我读过某个地方,可以使用本机编程语言(C),但无法获得任何解决方案.我怎样才能做到这一点? 预先感谢.

I used File Observer but it is giving notification after file deletion and in marshmallow it does not notify for deletion also. I referred this link for file observer. Somewhere I read it is possible using native programming language (C), but couldn't get any solution. How can I do this? Thanks in advance.

我已经尝试过:

@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. 垃圾箱使用.trash目录,该目录可能会也可能不会一直存在.请注意,通过Google评论可以看到Dumpster在许多设备上无法正常运行.
  2. 垃圾箱使用(我从代码中仅出于教育目的猜测它)是它自己的系统文件Handler,该文件使用service检查onClick事件以及是否是文件onClick会将File及其path保存到单独的文件夹(通常是隐藏的),也将其保存在本地的database中.如果将其删除,则您知道文件在哪里,如果不知道,则从该hidden folder中删除该文件.嗯,这有点不值得,因为您几乎需要使服务在几乎所有使用CPU资源的时间内都运行.它也可以在有根设备上运行,但是为什么仅为此目的使用根设备.
  3. 随着设备安全性的提高,执行这些任务的可能性越来越小.作为 2017年1月9日的最新版本,所有 文件回收站 都对最新的android版本具有负面评价.因此,证明我的观点.

  1. Dumpster Uses .trash directory which may and may not be present always. This should be noted that Dumpster does not run correctly in many devices as it can be seen through google reviews.
  2. Dumpster uses (i guessed it from the code for educational purpose only) it's own System File Handler that uses a service to check for the onClick event and if its a file onClick it saves the File as well as its path to a separate folder (usually hidden) and also saves it in a database that is local. If it is deleted you know where the file is and if not lets delete that file from that hidden folder. Well that's kinda not worth the pain because you need to almost make your service run for almost all the time which uses CPU resources. It also runs on rooted devices but why root device for this purpose only.
  3. As the Security in devices are increasing it is becoming less possible to perform these tasks. As latest of 1-09-2017 all these of files recycle bin have Negative reviews on latest android versions. Hence, proving my point.

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

FileObserver uses the concept for checking any changes on the file or even directories but you cannot influence it meaning you cannot prevent deletion it will notify everything after the user has deleted.

inotify.h 用于创建应用程序的NDK目的,用于检查文件夹和文件上的事件,但是如果提到了该文件夹,则子子文件夹将不在此范围内或通知您文件的任何更改.此外,inotify中使用的概念与FileObserver相同.您只有在删除文件后才能收到通知. inotify中使用的代码是这样的.

inotify.h it is the used for NDK purposes for creating application using to check the events on folders and files but if the folder is mentioned the sub sub folder will not be covered in this or notify you any change for the file. Moreover the concept used in inotify is same as FileObserver. you can only receive the notification after the file is deleted. The code used in the inotify is something like this.

  1. 通过inotify_init()创建inotify实例.
  2. 使用inotify_add_watch()函数将所有要监视的目录添加到inotify列表中.
  3. 要确定发生的事件,请在inotify实例上执行read().该读取将被阻止,直到发生更改事件为止.它 建议对此inotify实例执行选择性读取 使用select()调用.
  4. 读取返回受监视目录上发生的事件的列表.根据read()的返回值,我们将确切知道哪种 发生了变化.
  5. 如果要删除目录/文件上的监视,请调用inotify_rm_watch().
  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中存在的两种方法如下:

The two methods present in inotify is as follow:

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

IN_DELETE – File/directory deleted from watched directory

IN_DELETE_SELF –观看的文件/目录本身已删除

IN_DELETE_SELF – Watched file/directory was itself deleted

两者都与FileObserver

  1. 此解决方案可能无法完全起作用,但仍可以帮助创建任何 Dumpster 类型的应用程序. 可以说,您需要创建自己的File Manager,在其中可以创建扩展File的自定义FileV2(只是一个很酷的名称File version 2.0)类,并且您可以覆盖delete方法(以及所有其他方法) ) 随你心意.您可以创建一个custom pop up,是否要使用自己的backing up文件将delete文件设置为yes,而将dismissing弹出窗口设置为no. (确保用户使用此文件管理器进行删除,否则它将无法正常工作,因为覆盖系统文件delete()也会使其他应用程序混乱.)

  1. This Solution can be of help not fully but still can help in creating any Dumpster type Application. It can be said that you need to create your own File Manager where you can Create your own Custom FileV2(Just a cool name File version 2.0) class that extends File and you can override the delete method (and all others) as you like. You can create a custom pop up saying do you want to delete the file with your own backing up the file on yes and dismissing the pop up on no. (Make Sure User uses this File Manager to Delete otherwise it will not work because overriding the system File delete() will just mess up other applications as well).

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.
}

}

但是请确保如果用户为此使用您的File Manager,则将保存您的文件. 您可以为任务设置intent-filters,这样您的FileManager就会出现在ACTION_VIEW中.

But make sure your files will be saved if the user uses your File Manager for this. You can set the intent-filters for the task so that your FileManager comes in ACTION_VIEW for that matter.

最后,但是我不确定,也许registerContentObserver也可以使用. (虽然不确定)

Last but I am not sure about this maybe registerContentObserver can be used also. (Not Sure though)

来源:

Inotify.h帮助网站

registerContentObserver帮助

种类类似的问题

FileObserver帮助

Linux帮助已删除文件日志

我希望它会有所帮助,并希望您现在可以开始自己想要的东西.

I hope it helps and I hope you can now have a start to what you want.

这篇关于我如何获得从SD卡删除任何文件的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆