Override file delete in android

2019-07-15 08:10发布

问题:

Is it possible to override File.delete() function? Whenever i delete any file from sdcard from other apps (not using by my application) i need a notification like this file is going to be delete. A sample code snippet i tried is,

public class ExtendFile extends File
{
    public ExtendFile(File dir, String name) 
    {
         super(dir, name);
         // TODO Auto-generated constructor stub
    }

    @Override
    public boolean delete() 
    {    
        System.out.println("to be deleted");
        return super.delete();
     }
}  

and

File file = new File("/mnt/sdcard/tmp.txt");
ExtendFile f = new ExtendFile(file, tempPath);

i implemented above two statements in a service class in FileObsever. Now i deleted tmp.txt file from other app, but i didn't get any notification. How can i get the notifications from other apps ? Is it possible or not ?

Thanks in advance

回答1:

No, this is not possible. Not only will this cause problems with other applications but also with the Android OS.

With the FileObserver you can only see what has happened to a file. You cannot influence it. To conclude: If you only want to know what has happened to "/mnt/sdcard/tmp.txt" then use a FileObserver, but you cannot prevent deletion.