Android Monitor network status

Source: Internet
Author: User

HTML code
  1. public static Boolean isnetworkavailable (context context) {
  2. Connectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (context.connectivity_service  );
  3. if (connectivity = = null) {
  4. LOG.I ("Networkstate", "Unavailabel");
  5. return false;
  6. } else {
  7. networkinfo[] info = Connectivity.getallnetworkinfo ();
  8. if (info! = null) {
  9. for (int i = 0; I < info.length; i++) {
  10. if (info[i].getstate () = = NetworkInfo.State.CONNECTED) {
  11. LOG.I ("Networkstate", "Availabel");
  12. return true;
  13. }
  14. }
  15. }
  16. }
  17. return false;
  18. }



The above method is to determine whether the network connection code, return True indicates that there is a network, return false indicates no network. In the development of Android Web applications, it is often necessary to determine whether a network connection is available, so it is often important to listen for changes in network status. Android Network status monitoring can use Broadcastreceiver to receive network status changes broadcast, the implementation of the following:

HTML code
  1. @Override
  2. public void OnReceive (context context, Intent Intent) {
  3. LOG.E (TAG, "Network status change");
  4. Boolean success = false;
  5. Access to network connectivity services
  6. Connectivitymanager Connmanager = (connectivitymanager) getsystemservice (Connectivity_service);
  7. State state = connmanager.getactivenetworkinfo (). GetState ();
  8. State state = Connmanager.getnetworkinfo (
  9. Connectivitymanager.type_wifi). GetState (); Get Network Connection Status
  10. if (state.connected = = state) {//Determine if WiFi network is in use
  11. Success = true;
  12. }
  13. State = Connmanager.getnetworkinfo (
  14. Connectivitymanager.type_mobile). GetState (); Get Network Connection Status
  15. if (state.connected! = state) {//Determine if GPRS network is being used
  16. Success = true;
  17. }
  18. if (!success) {
  19. Toast.maketext (Locationmapactivity.this, "Your network connection has been interrupted", Toast.length_long). Show ();
  20. }
  21. }


In the OnCreate of activity:

HTML code
      1. //register Network Monitor   
      2. intentfilter < span class= "attribute" >filter = new intentfilter ();    
      3. filter.addaction (connectivitymanager.connectivity_action);   
      4. Registerreceiver (mnetworkstatereceiver, filter);   
      5. //OnDestroy in activity: '    
      6.   
      7. unregisterreceiver (mnetworkstatereceiver);  //Cancel monitoring  

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.