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.
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.
I have tried this:
@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";
}
}
Lets clarify the things First.
- 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
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
it is the used forNDK
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 ininotify
is same asFileObserver
. you can only receive the notification after the file is deleted. The code used in the inotify is something like this.
- 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
().
The two methods present in inotify is as follow:
IN_DELETE
– File/directory deleted from watched directory
IN_DELETE_SELF
– Watched file/directory was itself deleted
both of which are almost same as in FileObserver
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. } }
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.
Last but I am not sure about this maybe registerContentObserver
can be used also. (Not Sure though)
Sources:
Linux Help Deleted Log for Files
I hope it helps and I hope you can now have a start to what you want.
来源:https://stackoverflow.com/questions/44720696/how-can-i-get-notification-of-any-file-being-deleted-from-sd-card