1

I am not sure how this function works. This is prototype:

int ALooper_addFd(ALooper*looper, int fd, int ident, int events, ALooper_callbackFunc callback, void *data);

What is this 'data' pointer? If callback is not NULL, 'data' should be custom data passed to callback. But, if callback is NULL, what is 'data' parameter used for? Or it have to be NULL also, in that case?

Is there any detailed documentation about this? Thank you in advice!

4

1 回答 1

1

它是一个指向任意数据的指针,如果您在应用程序中需要它,您可以设置它。

除了作为参数发送给回调之外,当你调用or时,data指针也会被写入参数引用的内存中。outDataALooper_pollAllALooper_pollOnce

您可以以 Android Native App Glue 为例。在这种情况下,data指向android_poll_source由胶水定义的结构,并且为了使胶水将来自 looper 的文件描述符的命令转换为输入和APP_CMD事件,您必须在收到来自 looper 的事件时调用该process函数:(android_poll_source *)data

int ident, events;
struct android_poll_source *source; // source is the data here
while ((ident = ALooper_pollAll(0, NULL, &events, (void **)(&source))) >= 0) {
    if (source) {
        source->process(source->app, source);
    }
}
于 2015-03-29T19:10:48.290 回答