我们这里使用MulticastSocket以及组播锁的形式实时监控对应的ip和端口是否实时存在!
下面我们来看代码:
public Thread getListenThread(){
Thread thread = null;
try {
thread = new Thread() {
private static final String TAG = "ReceiverThread";
private static final String MULTICAST_IP = "239.255.255.250";
private static final int MULTICAST_PORT = 1900;
private WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
private MulticastSocket multicastSocket = new MulticastSocket(MULTICAST_PORT);
private InetAddress groupInetAddress = InetAddress.getByName(MULTICAST_IP);
private boolean joinedGroup = false;
@Override
public void run() {
while (!this.isInterrupted()) {
if (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
if (!joinedGroup)
try {
multicastSocket.joinGroup(groupInetAddress);
wifiManager.createMulticastLock(TAG).acquire();
joinedGroup = true;
} catch (IOException ex) {
Log.i(TAG, "Failed to join Multicast group:" + ex.getMessage());
}
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true){
multicastSocket.receive(packet);
String text = new String(buffer, 0, packet.getLength());;
// LogUtil.i(TAG, "Message received: /n" + text+"RECEIVED END");
Message msg = new Message();
msg.what = 4;
Bundle bundle = new Bundle();
bundle.putString("text", text);
msg.setData(bundle);
handlerdishes.sendMessageDelayed(msg,500);
}
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException ex) {
multicastSocket = null;
ex.printStackTrace();
}
} else
joinedGroup = false;
}
}
};
} catch (IOException e) {
e.printStackTrace();
}
return thread;
}
如上我们可知是使用线程的方式启动MulticastSocket,activity中启动的代码很简单:
/*
* 监听盒子状态信息
*/
Thread boxListener_thread = getListenThread();
boxListener_thread.start();
同时使用handler处理相对应的事件,就OK了!
需要的权限有如下:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />