美文网首页Android SAPT
监听网络状态变化

监听网络状态变化

作者: GuoBangbang | 来源:发表于2016-10-19 14:52 被阅读7次
    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>
    

    相关文章

      网友评论

        本文标题:监听网络状态变化

        本文链接:https://www.haomeiwen.com/subject/krsxuttx.html