PrivateConnectivitymanager Mconnectivitymanager; Privatenetworkinfo NetInfo, OnCreate registered broadcast Intentfilter Mfilter=NewIntentfilter (); Mfilter.addaction (connectivitymanager.connectivity_action); Registerreceiver (Mynetreceiver, mfilter); Be sure to unregister the broadcast in the OnDestroy () method @Override Public voidOnDestroy () {Super. OnDestroy (); if(mynetreceiver!=NULL) {unregisterreceiver (mynetreceiver); }}rl_error_item is a layout file similar to the QQ list display of the disconnected network state XML layoutPrivateBroadcastreceiver Mynetreceiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if(Action.equals (connectivitymanager.connectivity_action)) {Mconnectivityman Ager=(Connectivitymanager) Context.getsystemservice (Context.connectivity_service); NetInfo=Mconnectivitymanager.getactivenetworkinfo (); if(NetInfo! =NULL&&netinfo.isavailable ()) { //Network ConnectionString name =Netinfo.gettypename (); if(Netinfo.gettype () ==connectivitymanager.type_wifi | | netinfo.gettype () ==connectivitymanager.type_ethernet | | Netinfo.gettype () = =connectivitymanager.type_mobile) { //3g Networkrl_error_item.setvisibility (View.gone); } } Else { //Network Disconnectrl_error_item.setvisibility (view.visible); } } } };
View Code
Android Webcast similar to QQ dynamic Check network