我如何获得从SD卡删除任何文件的通知 [英] How can I get notification of any file being deleted from SD card
问题描述
我想创建类似应用程序的 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";
}
}
推荐答案
首先让我们澄清一下.
- 垃圾箱使用.trash目录,该目录可能会也可能不会一直存在.请注意,通过Google评论可以看到Dumpster在许多设备上无法正常运行.
- 垃圾箱使用(我从代码中仅出于教育目的猜测它)是它自己的系统文件
Handler
,该文件使用service
检查onClick
事件以及是否是文件onClick
会将File
及其path
保存到单独的文件夹(通常是隐藏的),也将其保存在本地的database
中.如果将其删除,则您知道文件在哪里,如果不知道,则从该hidden folder
中删除该文件.嗯,这有点不值得,因为您几乎需要使服务在几乎所有使用CPU资源的时间内都运行.它也可以在有根设备上运行,但是为什么仅为此目的使用根设备. -
随着设备安全性的提高,执行这些任务的可能性越来越小.作为 2017年1月9日的最新版本,所有 文件回收站 都对最新的android版本具有负面评价.因此,证明我的观点.
- 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.
- Dumpster uses (i guessed it from the code for educational purpose only) it's own System File
Handler
that uses aservice
to check for theonClick
event and if its a fileonClick
it saves theFile
as well as itspath
to a separate folder (usually hidden) and also saves it in adatabase
that is local. If it is deleted you know where the file is and if not lets delete that file from thathidden 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. 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.
- 通过
inotify_init
()创建inotify实例. - 使用
inotify_add_watch
()函数将所有要监视的目录添加到inotify
列表中. - 要确定发生的事件,请在inotify实例上执行read().该读取将被阻止,直到发生更改事件为止.它
建议对此inotify实例执行选择性读取
使用
select
()调用. - 读取返回受监视目录上发生的事件的列表.根据
read
()的返回值,我们将确切知道哪种 发生了变化. - 如果要删除目录/文件上的监视,请调用
inotify_rm_watch
().
- Create the inotify instance by
inotify_init
(). - Add all the directories to be monitored to the
inotify
list usinginotify_add_watch
() function. - 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. - 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. - 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
-
此解决方案可能无法完全起作用,但仍可以帮助创建任何 Dumpster 类型的应用程序. 可以说,您需要创建自己的
File Manager
,在其中可以创建扩展File
的自定义FileV2
(只是一个很酷的名称File version 2.0)类,并且您可以覆盖delete方法(以及所有其他方法) ) 随你心意.您可以创建一个custom
pop up
,是否要使用自己的backing up
文件将delete
文件设置为yes,而将dismissing
弹出窗口设置为no. (确保用户使用此文件管理器进行删除,否则它将无法正常工作,因为覆盖系统文件delete()也会使其他应用程序混乱.)
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 CustomFileV2
(Just a cool name File version 2.0) class that extendsFile
and you can override the delete method (and all others) as you like. You can create acustom
pop up
saying do you want todelete
the file with your ownbacking up
the file on yes anddismissing
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)
来源:
我希望它会有所帮助,并希望您现在可以开始自己想要的东西.
I hope it helps and I hope you can now have a start to what you want.
这篇关于我如何获得从SD卡删除任何文件的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!