Android about judging if the app has a network

Source: Internet
Author: User

There are many applications in Android programming need to determine whether there is a network problem, now I would like to say about the network, the source code is as follows:.java /**     *  Check that the current network is available      *      *  @param  context      *  @return      */         public boolean isnetworkavailable (activity activity)     {         context context = activity.getapplicationcontext ();         //  get phone all connection management objects (including management of connections such as Wi-fi,net)          ConnectivityManager connectivityManager =  (Connectivitymanager)  context.getsystemservice (Context.connectivity_service);                 if  (connectivitymanager == null)         &nbsP {            return false;         }        else         {            //  Get Networkinfo Objects             NetworkInfo[]  Networkinfo = connectivitymanager.getallnetworkinfo ();                         if  (networkinfo != null && networkinfo.length > 0)              {                 for  (int i = 0; i <  networkinfo.length; i++)                 {                      System.out.println (i +  "= = = Status = = ="  + networkinfo[i].getstate ());                      System.out.println (i +  "= = = Type = = ="  + networkinfo[i].gettypename ());                     //  Determine if the current network state is a connection state                      if  (Networkinfo[i].getstate ()  == networkinfo.state.connected)                      {       &nbSp;                return  true;                     }                 }            }         }        return false;     }
This method is judged in the OnCreate () method, with the following code:. java@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); if (isnetworkavailable (maintivity.this) { Toast.maketext (This, "there are currently available networks!") ", Toast.length_long). Show ();} else{Toast.maketext (This, "No network at this moment!"  ", Toast.length_long). Show ();}} Additionally, you need to add permissions <!--allow apps to be networked--<uses-permission android:name= "Android.permission.INTERNET"/> <!--allow App Check network status--<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission>


This article is from the "Android Network" blog, so be sure to keep this source http://3127279300.blog.51cto.com/10023287/1725343

Android about judging if the app has a network

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.