2012. 3. 21. 02:58

//////////////////////////////////////////

//웹소켓을 처리하는 도중 핸드폰의 특성상 네트워크환경을 많이 타게 된다. 그때문에 만들게 된 소스..

/////////////////////////////////////////////


BroadcastReceiver networkStateReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

// Log.e("Network Listener", "________Network Type Changed");

String action = intent.getAction();

if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.e("TAG", "_______Receiver onConnectivity");

NetworkInfo info = intent
.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

if (info == null) {
Log.e("TAG", "________No Network Info Available");

return;
}

Log.e("_network statues_", "___________"
+ info.getState().toString());
if (info.isConnected()) {

try {
// Log.e("aaaaaaa", "___" + getUser);

iosocket = new IOSocket("ws://115.68.23.219:3000",
getUser, service.this);
iosocket.connect();

} catch (IOException e) {
e.printStackTrace();
}

Log.e("TAG", "________Broadcast: Network Up");
// Restart any downloads.
// Send any pending transactions.
// Start any async tasks.
// (optional) Request partial wakelock to allow
// networking to continue.
return;
} else {
// iosocket.
try {
iosocket.getWebSocket().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("TAG", "________Broadcast: Network Down");

}
return;
}

}
};

IntentFilter filter = new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(networkStateReceiver, filter);

Posted by k1rha