在Android的替代文件中删除文件、Android

2023-09-07 14:58:02 作者:你是我的单曲循环

是否有可能重写File.delete()函数?每当我(而不是我的应用程序中使用)中删除从其他应用程序的SD卡的任何文件,我需要这样的文件通知,将是删除。样本code段我想是,

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();
     }
}  

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

我实现了上面的 FileObsever 服务类两种说法。 现在,我删除了其他应用程序tmp.txt文件,但我没有得到任何通知。 我怎样才能从其他应用程序的通知?是否有可能或不?

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 ?

在此先感谢

推荐答案

没有,这是不可能的。这不仅会引起问题与其他应用程序也与Android操作系统。

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

随着FileObserver你只能看到有发生在一个文件中。你不能影响它。 总之:如果你只是想知道发生了什么事/mnt/sdcard/tmp.txt然后用FileObserver,但你不能prevent删除

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.