編輯:Android開發教程
package com.chy.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; import com.chy.global.GlobalValue; public class UdpClient { private boolean isStop = false; private DatagramSocket datagramSocket = null; private onReceiveListener onReceiveListener = null; private ReadThread readThread = null; public interface onReceiveListener { public void onReceive(DatagramPacket packet); } class ReadThread extends Thread { @Override public void run() { byte[] buffer = new byte[GlobalValue.READ_BLOCK_SIZE]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); while (!isStop && !isInterrupted()) { try { datagramSocket.receive(packet); if (onReceiveListener != null) { onReceiveListener.onReceive(packet); } } catch (IOException ex) { ex.printStackTrace(); } } } } public UdpClient(int port, onReceiveListener onReceiveListener) { try { datagramSocket = new DatagramSocket(port); } catch (SocketException ex) { ex.printStackTrace(); } if (datagramSocket == null) { return; } this.onReceiveListener = onReceiveListener; readThread = new ReadThread(); readThread.start(); } public synchronized boolean send(byte[] buffer) { boolean result = false; if(datagramSocket != null) { DatagramPacket packet = new DatagramPacket(buffer, buffer.length); try { datagramSocket.send(packet); result = true; } catch (IOException ex) { ex.printStackTrace(); } } return result; } public void close() { isStop = true; if(readThread != null && !readThread.isInterrupted()) { readThread.interrupt(); readThread = null; } if (datagramSocket != null) { datagramSocket.close(); } } }
URL:http://www.bianceng.cn/OS/extra/201608/50409.htm
React Native Android gradle下載慢問題解決很多人會遇到 初次運行 react-native run android的時候 gradle下載極慢,
除了使用Notification之外,Toast也可以用來通知用戶某件事發生了,Toast為一個透明的對話框,可以在屏幕上顯示幾秒 鐘後自動消失。功能上有點類似Tool
這個例子和Translucent不同的一點是Blur,也就是顯示在當前Activit背後的其它Activity以模糊方式顯示。這是通 過window對象Flag來設置的。
通過roster可以獲得好友列表,前提是在服務器中建立了好友列表,不然沒有的話,不會獲取全部用戶的,roster.getEntries()的size將會為0。下面來看看怎