1

我正在尝试通过使用来监视“/sys/class/udc”文件夹FileObserver。我的应用程序是系统私有应用程序,因此不应该存在权限问题。我无法检测到更改事件。

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”文件的监控?

4

1 回答 1

2

通过 Android 框架代码后,我能够找到我的问题的答案。想通过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.
于 2017-09-02T13:07:31.557 回答