亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

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

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

// 獲取Mobile網(wǎng)絡下的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 都是中國電信定制機接入點名稱,中國電信的接入點:Net、Wap都采用Net即非代理方式聯(lián)網(wǎng)即可
   //internet 是模擬器上模擬接入點名稱
   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{
   //適配中國電信定制機,如海信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方式下獲取當前的網(wǎng)絡連接方式,代理或非代理
 * 
 */
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;
}
/**
 * 獲取手機卡類型,移動、聯(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"))
    {
      // 中國移動
    }
    else if (iNumeric.equals("46001"))
    {
      // 中國聯(lián)通
    }
    else if (iNumeric.equals("46003"))
    {
      // 中國電信
    }
  }
}

希望本文所述對大家Android程序設計有所幫助。

相關(guān)文章

最新評論