欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程獲取網(wǎng)絡(luò)連接方式及判斷手機(jī)卡所屬運(yùn)營商的方法

 更新時(shí)間:2016年01月12日 10:42:34   作者:hshm20517  
這篇文章主要介紹了Android編程獲取網(wǎng)絡(luò)連接方式及判斷手機(jī)卡所屬運(yùn)營商的方法,涉及Android針對(duì)網(wǎng)絡(luò)的判斷及本機(jī)信息的獲取技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程獲取網(wǎng)絡(luò)連接方式及判斷手機(jī)卡所屬運(yùn)營商的方法。分享給大家供大家參考,具體如下:

問題:項(xiàng)目中寫的網(wǎng)絡(luò)模塊,感覺有點(diǎn)亂:兩套代碼 --模擬器、真機(jī),維護(hù)起來十分麻煩。

解決辦法:代碼自動(dòng)去檢查到那種網(wǎng)絡(luò)環(huán)境,然后調(diào)用不同的聯(lián)網(wǎng)方式。

查看了模擬器上默認(rèn)的接入點(diǎn):移動(dòng)網(wǎng)絡(luò) -- APN = "internet"

1、通過獲取apn的名稱,來判斷網(wǎng)絡(luò)

// 獲取Mobile網(wǎng)絡(luò)下的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){//適配平板外掛3G模塊情況
   if (!cursor.isAfterLast()) {
   String apn = cursor.getString(1);
   //#777、ctnet 都是中國電信定制機(jī)接入點(diǎn)名稱,中國電信的接入點(diǎn):Net、Wap都采用Net即非代理方式聯(lián)網(wǎng)即可
   //internet 是模擬器上模擬接入點(diǎn)名稱
   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{
   //適配中國電信定制機(jī),如海信EG968,上面方式獲取的cursor為空,所以換種方式
   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;//平板外掛3G,采用非代理方式上網(wǎng)
  }
  cursor.close();
  return type;
}

2、直接獲取代理參數(shù):proxy 來判斷是否為代理

/**
 * MOBILE方式下獲取當(dāng)前的網(wǎng)絡(luò)連接方式,代理或非代理
 * 
 */
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;
}
/**
 * 獲取手機(jī)卡類型,移動(dòng)、聯(lián)通、電信
 * 
 */
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"))
    {
      // 中國移動(dòng)
    }
    else if (iNumeric.equals("46001"))
    {
      // 中國聯(lián)通
    }
    else if (iNumeric.equals("46003"))
    {
      // 中國電信
    }
  }
}

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論