I use comet style communication with my android app. It worked all fine until I used Gingerbread (Emulator, CyanogenMod 7). The problem I have is that
Selector selector = Selector.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
channel.socket().setKeepAlive(true);
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
if(selector.select() > 0){
//DO STUFF
}
the
selector.select()>0
returned true if I sent an event from the server but now it just ignores it. Exactly the same code w开发者_StackOverflow中文版orks in Android 1.6 - 2.2
I think this is a bug... someone can confirm the same issue or provide a workaround?
i found the solution, but not why it worked before.
channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);
does not work anymore.
I had to register another selector only dealing with OP_READ and now it works.
channel.register(readSelector, SelectionKey.OP_READ);
channel.register(connectSelector, SelectionKey.OP_CONNECT);
if(connectSelector.select() > 0){
//Do connect stuff
} else if(readSelector.select() > 0){
//Do read stuff
}
I hope this saves some headaches.
EDIT: http://code.google.com/p/android/issues/detail?id=15055
I posted it as a issue on the android issuetracker and it is under investigation
I have run into a similar issue. When I try to send something the selector does not get out of the blocking mode. So I can only recieve the network packets and not send any. I will give this a shot and let you know. I am running android 2.3.3 on a nexus one.
精彩评论