Android 3G connection status judgment

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.