Most Android mobile phones support WiFi, GSM, and 3G networks, but each connection can only be used. Some programs (such as customized by the operator) must also require the use of specific networks (such as cmwap and ctwap). If the network connected to the current mobile phone is not the specified network, in this case, the program may jump to the network settings interface for users to manually set up, or it may be that the program automatically switches to the network (switching from cmnet to cmwap ). The process of switching the network is to first shut down the current network and then start a new network. This process takes some time. Maybe the program needs to know when the new network connection is successful and then automatically log on. How can we know when the connection is successful? How long does it take?
Maybe it is feasible to use a thread to listen to the network status. I have not tried it. We can use another method. phonestatelistener is correct. If you are not mistaken, you can use phonestatelistener. Many applications use phonestatelistener to listen for incoming call power. phonestatelistener can also listen for network disconnection, connection, and connection success. Let's look at the code. Java code: Final telephonymanager mtelephonymgr = (telephonymanager) getsystemservice (context. telephony_service );
Mtelephonymgr. listen (New phonestatelistener () {@ override public void ondataconnectionstatechanged (INT state) {Switch (state) {Case telephonymanager. data_disconnected: // network disconnection break; Case telephonymanager. data_connecting: // The network is connecting to break; Case telephonymanager. data_connected: // network connection
Break ;}}, phonestatelistener. listen_data_connection_state); If the ondataconnectionstatechanged method is reloaded, the corresponding processing is performed based on the state judgment.