如何在Android中使用FileObserver监控" / sys / class /"

时间:2017-08-16 08:58:38

标签: android fileobserver

我正在尝试监控" / sys / class / udc"使用FileObserver的文件夹。我的应用程序是一个system-priv应用程序,所以不应该有权限问题。我无法检测到更改事件。

String path = "/sys/class/udc";
static final int mask = (
    FileObserver.CREATE |
    FileObserver.DELETE |
    FileObserver.DELETE_SELF |
    FileObserver.MODIFY |
    FileObserver.MOVED_FROM |
    FileObserver.MOVED_TO |
    FileObserver.MOVE_SELF
);

public UsbCableDisconncetEvent(String path) {
    super(path, mask);
    Log.d(TAG, "DisconnectEventHandler modified");
}

public void onEvent(int event, String path) {
    Log.d(TAG, "recieved moified event " + event);
}

FileObserver是否支持监听" / sys / class"文件?

1 个答案:

答案 0 :(得分:0)

在浏览Android Framework代码后,我能够找到问题的答案。那些想要使用FileObesever监视内核事件( uevent)的人可以参考以下代码:

private static final String STATE_PATH = "/sys/class/android_usb/android0/state";
public final UEventObserver mUEventObserver = new UEventObserver() {
       @Override
        public void onUEvent(UEventObserver.UEvent event) {
            Log.d(TAG, "USB UEVENT: " + event.toString());
        }
    };
mUEventObserver.startObserving(USB_STATE_MATCH);

You will start receiving events and should be able to see the same in the logs.