You want to obtain the android network events
First, you must register a explorer.
< Cycler Android: Name = "Com. Ray. Ray. Receiver. connectionchangereceiver" Android: Label = "Networkconnection" > < Intent-Filter > < Action Android: Name = "Android.net. Conn. connectivity_change" /> < Action Android: Name = "Android. Intent. Action. user_present" /> </ Intent-Filter > </ Cycler >
When the network status changes, the android.net. Conn. connectivity_change broadcast is sent.
The android. Intent. Action. user_present broadcast will be sent when the mobile phone is unlocked. In order to get it when the mobile phone is unlocked, the broadcast of the mobile phone unlock event will be added.
After registering a consumer, you can receive a broadcast of network status changes and mobile phone unlock actions in connctionchangereceiver.
Next, we can process the received broadcast by adding the network status judgment in onreceive as follows:
Connectivitymanager connectmgr =(Connectivitymanager) Context. getsystemservice (context. connectivity_service); networkinfo mobnetinfo=Connectmgr. getnetworkinfo (connectivitymanager. type_mobile); networkinfo wifinetinfo=Connectmgr. getnetworkinfo (connectivitymanager. type_wifi );
Connectivitymanager obtains the current system service information
Getnetworkinfo obtains the information of two network modes, mobile and WiFi.
Determine the current network status by judging the Boolean values returned by mobnetinfo. isconnected () and wifinetinfo. isconnected.
If(! Mobnetinfo. isconnected ()&&!Wifinetinfo. isconnected ()){//Unconnect Network}Else{//Connect Network}
Only whether the network is connected or not is added here. If you need to distinguish between data and Wi-Fi connections, you can determine whether to connect to the network separately.
Note that the permission to access Android permission is added.
<Uses-PermissionAndroid: Name= "Android. Permission. access_network_state" />
The preceding figure shows the network status of the android listener.CodeImplementation Process
RequiredSource codeCan be obtained in the following two ways:
(1) http://download.csdn.net/detail/fnext/5228714
(2) enter the email address in the comment and the source code will be sent through the email address.
//////////////////////////////////////// //////
Reprinted original statement, please note
This article is from Ray-Ray's blog
ArticleAddress http://www.cnblogs.com/rayray/archive/2013/03/25/2965474.html
Thank you for your recommendation and favorites.
Your support! Our motivation!