public class NetStateReceiver extends BroadcastReceiver {
private static final String TAG = NetStateEvent.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
NetStateEvent stateEvent;
if (NetWorkUtils.isNetworkAvailable(context)) {
stateEvent = new NetStateEvent(true);
} else {
stateEvent = new NetStateEvent(false);
}
EventBus.getDefault().post(stateEvent);
}
}
<receiver android:name="com.example.NetStateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
网友评论