Android Network Connection Tool

Source: Internet
Author: User

Android Network Connection Tool

Public class NetUtil {/*** check the current network */public static boolean checkNet (Context context) {// determine whether the current user's mobile phone uses WIFI to communicate boolean isWIFI = isWIFIConnection (context); // determine whether the current user's mobile phone uses APN to communicate boolean isAPNConnection (context ); // No -- Prompt User -- jump to the network to set if (isAPN = false & isWIFI = false) {return false;} if (isAPN) {// Access Point // IP address is 10.0.0.172 port is 80 read from the mobile phone configuration information // some mobile phones: 010.000.000.172setAPN (context);} return true ;}/*** Read */private static void setAPN (Context context) {Uri PREFERRED_APN_URI = Uri from the phone configuration information. parse ("content: // telephony/carriers/preferapn"); ContentResolver contentResolver = context. getContentResolver (); Cursor query = contentResolver. query (PREFERRED_APN_URI, null, null); // obtain the information of the currently connected APN if (query! = Null & query. moveToNext () {// sets the ip address and port information GlobalParams. IP = query. getString (query. getColumnIndex ("proxy"); GlobalParams. PORT = query. getInt (query. getColumnIndex ("port") ;}}/*** determine whether WIFI is connected ** @ return */private static boolean isWIFIConnection (Context context) {ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager. getNet WorkInfo (ConnectivityManager. TYPE_WIFI); if (networkInfo! = Null) return networkInfo. isConnected (); return false;}/*** determine whether the APN is connected ** @ return */private static boolean isAPNConnection (Context context) {ConnectivityManager manager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); if (networkInfo! = Null) return networkInfo. isConnected (); return false ;}}

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.