When developing Android applications, it involves network access, often requiring network state checks to provide users with the necessary reminders. It is generally possible to complete the work by Connectivitymanager.
Connectivitymanager has four main tasks:
1, monitor the status of mobile phone network (including Gprs,wifi, UMTS, etc.)
2, mobile phone status change, send broadcast
3, when a network connection fails to failover
4. Provide applications with high precision and rough state of access to available networks when we are listening to the network state in the program, just a few steps:
1, define a receiver overload of the OnReceive function, in which to complete the required functions , such as based on WiFi and GPRS is disconnected to change the appearance of the space
Connectionreceiver = new Broadcastreceiver () {
@Override public
void OnReceive (context context, Intent Intent) {
Connectivitymanager connectmgr = (connectivitymanager) getsystemservice (connectivity_service);
Networkinfo mobnetinfo = Connectmgr.getnetworkinfo (connectivitymanager.type_mobile);
Networkinfo wifinetinfo = Connectmgr.getnetworkinfo (Connectivitymanager.type_wifi);
if (!mobnetinfo.isconnected () &&!wifinetinfo.isconnected ()) {
log.i (TAG, "Unconnect");
Unconnect network
}else {
//Connect Network
}}}
;
2, in the appropriate place to register receiver, you can register in the program, in OnCreate call the following functions can be:
Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction (connectivitymanager.connectivity_action);
Registerreceiver (Connectionreceiver, Intentfilter);
3, cancel the registration receiver when appropriate, can cancel in the program, call the following function in Ondestroye:
if (connectionreceiver!= null) {
unregisterreceiver (connectionreceiver);
}
Ps: There are many ways to use Telephonymanager on the Internet, the following methods (but I have tried several times have problems, such as the first time to enter an activity will automatically receive the signal of network disconnect, each time the network state changes to receive multiple callbacks and the state is not correct.) Do not know what to pay attention to the place, seek advice! )
Final Telephonymanager mtelephonymgr = (telephonymanager) getsystemservice (context.telephony_service);
Mtelephonymgr.listen (New Phonestatelistener () {
@Override public
void ondataconnectionstatechanged (int State) {The
switch (state) {case
telephonymanager.data_disconnected://network disconnect break
;
Case telephonymanager.data_connecting://Network is connecting break
;
Case telephonymanager.data_connected://network connection break
;}
}
, Phonestatelistener.listen_data_ Connection_state);
As for the second method, I have not tried. The first way is still relatively easy to use, if you want to program hidden in the background, it is recommended to open a service, will broadcastreceiver registered in the service, but do not forget to cancel the registration.
In the test, it was possible to shut down a routing device that is currently connected to WiFi, but the program did not capture Unconnect network, possibly because the device immediately connected to another routing device.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.