The method of acquiring network connection and judging the carrier of mobile card by Android programming _android

Source: Internet
Author: User

This article illustrates the method of Android programming to get network connection and the way to judge the carrier of mobile card. Share to everyone for your reference, specific as follows:

Problem: The network module written in the project, feel a bit messy: two sets of code-simulator, real machine, maintenance is very troublesome.

Workaround: The code automatically checks that network environment and then calls different networking methods.

View the default access point on the emulator: Mobile network--APN = "Internet"

1, by obtaining the name of APN, to judge the network

Gets the Cmwap, cmnet private int getcurrentapninuse () {int type = Nonet under the mobile network; Cursor Cursor = Context.getcontentresolver (). Query (Preferred_apn_uri, new string[] {"_id", "APN", "type"}, NULL, n
  ull, NULL);
  Cursor.movetofirst ();
  int counts = Cursor.getcount ();
   if (counts!= 0) {//Fit flat panel 3G module condition if (!cursor.isafterlast ()) {String APN = cursor.getstring (1); #777, Ctnet are the name of China Telecom Customization Machine access point, China Telecom's access point: NET, WAP are used net that is not agent network can//internet is the simulator on the analog access point name if (apn.equalsignorecase) ("Cmne T ") | | Apn.equalsignorecase ("3gnet") | | Apn.equalsignorecase ("uninet") | | Apn.equalsignorecase ("#777") | | Apn.equalsignorecase ("ctnet") | |
   Apn.equalsignorecase ("Internet")) {type = Wifiandcmnet; else if (apn.equalsignorecase ("Cmwap") | | | apn.equalsignorecase ("3gwap") | | apn.equalsignorecase ("UNIWAP")) {type =
   Cmwap; }else{//Suitable for China Telecom custom machine, such as Hisense EG968, the above way to obtain the Cursor is empty, so the Cursor C = context.getcontentresolver (). Query (Preferred_apn_u
   Ri,null, NULL, NULL, NULL); C.moVetofirst ();
   String user=c.getstring (C.getcolumnindex ("user"));
   if (User.equalsignorecase ("Ctnet")) {type = Wifiandcmnet;
  } c.close ();
  }else{type = wifiandcmnet;//tablet 3G, using a non-proxy way to the Internet} cursor.close ();
return type;

 }

2, get proxy parameters directly: Proxy to determine whether the proxy

/** * Mobile mode gets the current network connection mode, proxy or non proxy */public static String Getcurrentapninuse (conte  XT context) {Cursor Cursor = context.getcontentresolver (). Query (Preferred_apn_uri, new string[] {"_id", "APN", "type",
  "Proxy"}, NULL, NULL, NULL);
  Cursor.movetofirst ();
  if (Cursor.isafterlast ()) {String APN = cursor.getstring (3);
  if (APN = = null) {APN = "";
} return APN;
  /** * Get Mobile card type, mobile, Unicom, telecom * * */private static int Getmobiletype (context context) {int type = 0;
  Telephonymanager Iphonemanager = (telephonymanager) context.getsystemservice (Context.telephony_service);
  String inumeric = Iphonemanager.getsimoperator ();
    if (inumeric.length () > 0) {if (Inumeric.equals ("46000") | | inumeric.equals ("46002")) {//China Mobile} else if (inumeric.equals ("46001")) {//China Unicom} else if (Inumeric.equals ("46003")) {//China power Letter}} 

I hope this article will help you with your Android programming.

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.