package cn.mucang.android.community.esa.library.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
/**
* Created by Sanders on 2014/11/4.
*/
public class ChangeReceiver extends BroadcastReceiver {
private static final String ACTION_CONNECTIVITY_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE";
/**
* 加入权限 uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
* 接受广播 action android:name="android.net.conn.CONNECTIVITY_CHANGE"
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_CONNECTIVITY_CHANGE)) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (activeNetInfo != null) {
Log.d("TOOL", "wifi状态 : " + wifiNetInfo.getState() + " 数据网络状态 : " + mobNetInfo.getState());
if (mobNetInfo.getState() == NetworkInfo.State.CONNECTED) {
Log.d("TOOL", "正在使用数据网络连接");
} else if (wifiNetInfo.getState() == NetworkInfo.State.CONNECTED) {
Log.d("TOOL", "正在使用wifi连接");
}
} else {
Log.d("TOOL", "没有连接任何网络");
}
}
}
}