class NetCheckReceiver extends BroadcastReceiver{
//android 中网络变化时所发的Intent的名字
public static final String netACTION = "android.net.conn.CONNECTIVITY_CHANGE";
@Override
public void onReceive(Context context, Intent intent){
if(intent.getAction().equals(netACTION)){
Log.e(TAG, "ACTION:" + intent.getAction());
//Intent中ConnectivityManager.EXTRA_NO_CONNECTIVITY这个关键字表示着当前是否连接上了网络
//true 代表网络断开 false 代表网络没有断开
boolean isBreak = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Log.e(TAG, "is break:" + isBreak);
}
}
}
转载于:https://blog.51cto.com/377069/822504