Você não vai conseguir fazer assim.
Procure por WebSockets que é o que você precisa mas já aviso que não é tão trivial se você não sabe muito de java.
Uma solução seria usar alguma coisa de terceiros ou usar o CGM para fazer um push e avisar o device para sincronizar.
Abraços,
Pedro Subutzki
__________________________________________
Pedro Subutzki
__________________________________________
HADI - Makes SQLite in Android easy and simple
https://github.com/PepeuCps/Hadi
https://github.com/PepeuCps/Hadi
2013/9/28 Luciano Coelho <lvctecno@gmail.com>
Boa tarde pessoal.--Não sou proficiente no java e lendo os tutoriais consegui fazer um servidor usando framework Netty se comunicar com um cliente Android.No lado server fiz assim:class Server extends ChannelInboundHandlerAdapterpublic void run() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Override public void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ServerHandler(service));}}).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15000).option(ChannelOption.TCP_NODELAY, false).childOption(ChannelOption.TCP_NODELAY, false).childOption(ChannelOption.SO_KEEPALIVE, true);ChannelFuture f = b.bind(port).sync();f.channel().closeFuture().sync();} catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex);}}class ServerHandler extends ChannelInboundHandlerAdapter
@Override public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {bufferIn = (ByteBuf) msg;int service = bufferIn.readInt();int size = bufferIn.readInt();try {while (bufferIn.isReadable()) { // (1)received = (char) bufferIn.readByte();result += received;}} finally {bufferIn.release();}System.out.println("RECEBEU: " + service + " SIZE: " + size + " MSG: " + result);if(size == result.trim().length()){data = new Worker().execute(service, result);}size = data.length();bufferOut = Unpooled.buffer(4+size);bufferOut.writeInt(size);bufferOut.writeBytes(data.getBytes());final ChannelFuture f = ctx.writeAndFlush(bufferOut); // (3)f.addListener(new ChannelFutureListener() {@Override public void operationComplete(ChannelFuture future) {assert f == future;ctx.flush();}}); // (4)System.out.println("ENVIOU: " + size + " " + data);}@Override public void channelActive(final ChannelHandlerContext ctx) { // (1)ctx.flush();}No lado cliente:class ConexaoNettypublic ConexaoNetty(String host, int port, int service, String message){this.host = host;this.port = port;this.service = service;this.message = message;this.size = message.length();}public void run() throws Exception {EventLoopGroup workerGroup = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, false).handler(new ChannelInitializer<SocketChannel>() {@Override public void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new ClientHandler(service, size, message));}});b.connect(host, port).sync();}class ClientHandler extends ChannelInboundHandlerAdapterpublic ClientHandler(int parService, int parSize, String parMessage, ConexaoListener parListener) {this.service = parService;this.size = parSize;this.message = parMessage;this.listener = parListener;}@Override public void channelActive(ChannelHandlerContext ctx) {this.bufferOut = Unpooled.buffer(4+size);this.bufferOut.writeInt(service);this.bufferOut.writeInt(size);this.bufferOut.writeBytes(message.getBytes());ctx.writeAndFlush(bufferOut);Log.d("WEB", "ENVIOU: " + service + " - " + size);}@Override public void channelRead(ChannelHandlerContext ctx, Object msg) {ByteBuf m = (ByteBuf) msg;bufferIn.writeBytes(m);m.release();try {if (bufferIn.readableBytes() >= 4) {size = bufferIn.readInt();result = bufferIn.toString(CharsetUtil.ISO_8859_1);if(size == result.trim().length()){System.out.println(result);}ctx.flush();}} finally {bufferIn.release();}}Então tenho duas classes uma de conexão que envia as mensagens e outra que é o handler onde recebe, para fazer o envio: new ConexaoNetty(ip_servidor, porta, service, data).run();A impressão que eu tenho é que toda vez q faço a solicitação de envio, estou abrindo uma nova conexão no servidor, certo? E o que eu quero é que ele faça a conexão uma unica vez e depois de conectado somente fique enviando para o servidor, sem fechar a conexão. Como fazer para distribuir essa conexão uma vez aberta, para todas as activities, como se fosse uma chamada de um método estatico, checando se a conexão está ativa (se estiver envia, senão estiver conecta), e como que eu faço para receber as respostas direto nas acitivities e não centralizada na classe ClientHandler?De antemão obrigado a todos por qualquer ajuda;
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.






0 comentários:
Postar um comentário