Android program to obtain the network connection mode and determine the mobile phone card operator

  • 2020-12-16 06:08:04
  • OfStack

The example of this paper describes the Android programming to obtain the network connection mode and determine the mobile phone card operator. To share for your reference, the details are as follows:

Problem: The network module written in the project feels a bit messy: two sets of code -- simulator and real machine, 10 minutes trouble to maintain.

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

Check the default access point on the simulator -- APN = "internet"

1. Judge the network by getting the name of apn


//  To obtain Mobile Under the network of cmwap , cmnet
private int getCurrentApnInUse() {
  int type = NONET;
  Cursor cursor = context.getContentResolver().query(PREFERRED_APN_URI,
      new String[] { "_id", "apn", "type" }, null, null, null);
  cursor.moveToFirst();
  int counts = cursor.getCount();
  if(counts != 0){// Fit for flat panel plug-in 3G The module is 
   if (!cursor.isAfterLast()) {
   String apn = cursor.getString(1);
   //#777 , ctnet  Are China Telecom custom machine access point name , China Telecom Access point: Net , Wap All use Net That is, non - proxy network can be 
   //internet  Is the name of the simulated access point on the simulator 
   if (apn.equalsIgnoreCase("cmnet") || 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, Obtained in the above way cursor It's null, so let's do it the other way 
   Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,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 plugins 3G, Use non-proxy Internet access 
  }
  cursor.close();
  return type;
}

2. Directly obtain the proxy parameter: proxy to determine whether it is a proxy


/**
 * MOBILE Gets the current network connection mode, proxy or non-proxy 
 * 
 */
public static String getCurrentApnInUse(Context 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 cell phone 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 Telecom 
    }
  }
}

I hope this article has been helpful in Android programming.


Related articles: