Android determines network connection, SIM card information, and whether the ping operation is connected properly

Source: Internet
Author: User

  //determine if the WiFi connection is     public boolean iswificonnected (Context context )  {        if  (context != null)  {             connectivitymanager mconnectivitymanager  =  (Connectivitymanager)  context                     .getsystemservice (Context.CONNECTIVITY_SERVICE);             networkinfo mwifinetworkinfo  = mConnectivityManager                     .getnetworkinfo (Connectivitymanager.type_wifi);             if  (mwifinetworkinfo != null)   {     &NBsp;          return mwifinetworkinfo.isavailable ();             }         }        return false;    }     //determine if the network connection is normal     public boolean isnetworkconnected (Context  context)  {        if  (context != null)   {            connectivitymanager  mconnectivitymanager =  (Connectivitymanager)  context                     .getsystemservice ( Context.connectivity_service);             Networkinfo mnetworkinfo = mconnectivitymanager                     .getactivenetworkinfo ();             if  (mnetworkinfo != null)  {                 return mnetworkinfo.isavailable ();             }        }         return false;    }    / /Determine network connection type     public static int getconnectedtype (context context)   {        if  (context != null)  {             connectivitymanager mconnectivitymanager =   (Connectivitymanager)  context                     .getsystemservice (Context.connectivity_service);             NetworkInfo mNetworkInfo = mConnectivityManager                     . Getactivenetworkinfo ();            if  ( Mnetworkinfo != null && mnetworkinfo.isavailable ())  {                 return mnetworkinfo.gettype ();             }         }        return -1;    }          //Determine the SIM card type of the mobile phone     public static string get (Context  Context)  {        TelephonyManager telManager =  ( Telephonymanager)  context                 .getsystemservice (Context.telephony_service);         string operator = telmanager.getsimoperator ();         if  (operator != null)  {             if  (Operator.equals ("46000")  | |  operator.equals ("46002"))  {                 //  China Mobile                  return  "Move";             } else if  (Operator.equals ("46001"))  {                 return  "Unicom";                 //  China Unicom              } else if  (Operator.equals ("46003") )  {                return   "Telecommunications";                 //   China Telecom             }         }        return  "";     }      //determine whether the phone can be normal internet   ping,  even if the phone is connected to the WiFi (hotspot no internet), mobile phone or not online     &nBsp;private static final boolean ping ()  {          string result = null;         try  {         String ip =  "www.baidu.com";//   Unless Baidu hangs, otherwise use this should be no problem ~         process p =  Runtime.getruntime (). EXEC ("ping -c 3 -w 100 "  + ip)//ping3 times           //  read the contents of ping without adding.          inputstream input = p.getinputstream ();          BufferedReader in = new  BufferedReader (New inputstreamreader (input));          Stringbuffer stringbuffer = new stringbuffer ();          string content =  "";          while  ((Content = in.readline ())  != null)  {          stringbuffer.append (content);          }          LOG.I ("TTT",  "result content : "  + stringbuffer.tostring ());         // ping status          int status = p.waitfor ();         if  (status == 0)  {         result =  "successful~";        return true;         } else {        result  =  "failed~ Cannot reach the ip address ";        }          } catch  (ioexception e)  {              result =  "Failed~ ioexception";              } catch  ( Interruptedexception e)  {              result =  "Failed~ interruptedexception";              } finally {              LOG.I ("TTT",  "result = "  + result);              }              return false;             } 





Can be used in combination with the above methods to achieve their own needs. The first blog post!!!!!!


Good Kai Sen!!!!!!!!! There's wood and!!!!!!!!!!!!!!!!!!!!. Ah, haha,!!!!!!!. Will someone find me/doubt/nose

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.