简体   繁体   English

在Android中覆盖文件删除

[英]Override file delete in android

Is it possible to override File.delete() function? 是否可以覆盖File.delete()函数? 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. 每当我从其他应用程序(不由我的应用程序使用)从sdcard删除任何文件时,我都需要通知,例如该文件将被删除。 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 . 我在FileObsever的服务类中实现了以上两个语句。 Now i deleted tmp.txt file from other app, but i didn't get any notification. 现在,我从其他应用程序中删除了tmp.txt文件,但没有收到任何通知。 How can i get the notifications from other apps ? 我如何从其他应用程序获得通知? Is it possible or not ? 有可能吗?

Thanks in advance 提前致谢

No, this is not possible. 不,这是不可能的。 Not only will this cause problems with other applications but also with the Android OS. 这不仅会导致其他应用程序出现问题,还会导致Android OS出现问题。

With the FileObserver you can only see what has happened to a file. 随着FileObserver你只能看到发生什么事到一个文件中。 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. 结论:如果只想知道“ /mnt/sdcard/tmp.txt”发生了什么,请使用FileObserver,但不能阻止删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM