connectivityReceiver = new ConnectivityReceiver(this);
这个类也是继承了BroadCastReceiver类,看他的onReceive方法就好:
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOGTAG, "ConnectivityReceiver.onReceive()...");
String action = intent.getAction();
Log.d(LOGTAG, "action=" + action);
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
Log.d(LOGTAG, "Network Type = " + networkInfo.getTypeName());
Log.d(LOGTAG, "Network State = " + networkInfo.getState());
if (networkInfo.isConnected()) {
Log.i(LOGTAG, "Network connected");
notificationService.connect();
}
} else {
Log.e(LOGTAG, "Network unavailable");
notificationService.disconnect();
}
}
主要就是输出一下网络的连接形式,网络状态,如果网络连接正常,就connetct,否则disconnect。
看看connnect方法。