InputChannel提供了一个通道,一方写入InputMessage ,一方读取。
以下演示了在同一个进程中,InputChannel的使用。
#include <array>
#include <utils/threads.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <binder/Binder.h>
#include <binder/Parcel.h>
#include <input/InputTransport.h>
#include <utils/StopWatch.h>
#include <utils/StrongPointer.h>
#include <utils/Timers.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
typedef uint8_t u8;
typedef int32_t u32;
using namespace std;
using namespace android;
int main(int, const char**) {
sp<InputChannel> serverChannel, clientChannel;
status_t result = InputChannel::openInputChannelPair("channel name",
serverChannel, clientChannel);
cout << serverChannel->getName() << endl;
cout << clientChannel->getName() << endl;
// Server->Client communication
InputMessage serverMsg;
memset(&serverMsg, 0, sizeof(InputMessage));
serverMsg.header.type = InputMessage::Type::KEY;
serverMsg.body.key.action = AKEY_EVENT_ACTION_DOWN;
serverMsg.body.key.keyCode = 123;
serverChannel->sendMessage(&serverMsg);
InputMessage clientMsg;
clientChannel->receiveMessage(&clientMsg);
if (serverMsg.header.type == clientMsg.header.type) {
cout << "OK" << endl;
} else {
cout << "no," << endl;
}
if (serverMsg.body.key.action == clientMsg.body.key.action) {
cout << "OK" << endl;
} else {
cout << "no," << endl;
}
cout << "clientMsg.body.key.keyCode: " <<clientMsg.body.key.keyCode << endl;
printf("end\n");
return 0;
}
后面会继续研究InputChannel跨进程的通信。