java - Android:FileObserver 不会收到有关 FS 更改的通知

标签 java android service fileobserver

我需要在手机启动后一直跟踪从整个文件系统中删除的图像和视频(即使我的 UI 应用程序未运行),直到其关闭。为此,我使用服务和 FileObserver。不幸的是,下面附加的代码不起作用(我没有使用调试器来获取该方法

public void onEvent(int 事件,字符串文件)

虽然我对 FS 做了一些更改...服务启动了并且显然执行了 onStarted() 方法...

请帮忙

public class DataChangeListener extends Service {

    private static Context context;

    @Override
    public IBinder onBind(Intent intent) {
        context = this;
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        context = this;

        FileObserver observer = new FileObserver("/") {

            @Override
            public void onEvent(int event, String file) {
                System.out.println();
            }
        };
        observer.startWatching();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}

最佳答案

I need to track removed images and videos from the whole file system all the time

这是不可能的,除非您创建操作系统的修改版本,因为您将无法保持服务始终运行。例如,用户可以随时删除您的服务。

Unfortunately the code attached below doesn't work

首先,您没有对 / 的读取权限,这可能会干扰 FileObserver 操作。

其次,您错过了突出显示的段落 in the FileObserver documentation :

Warning: If a FileObserver is garbage collected, it will stop sending events. To ensure you keep receiving events, you must keep a reference to the FileObserver instance from some other live object.

您允许对 FileObserver 进行垃圾收集。

关于java - Android:FileObserver 不会收到有关 FS 更改的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521464/

相关文章:

Android:远程服务中的主线程是什么?服务中的网络操作

java和内存布局

java - 如何使用JSP从URL中获取参数

android - 如何分配android :sharedUserId in the manifest?

java - 在 android 中使用 itext 生成 Pdf 用于 Bangla 文本

安卓NFC启动服务

java - JRadioButton 内的 JComboBox

java - 如何创建使用 JavaFX 的默认异常处理程序?

Android 小部件不会更新

android - 如何不打开已通过最近的应用程序完成的 Activity