原文 : http://www.cnblogs.com/growing/archive/2010/11/19/1881372.html
手機號碼不是所有的都能擷取。只是有一部分可以拿到。這個是由於行動電信業者沒有把手機號碼的資料寫入到sim卡中.SIM卡只有唯一的編號,供網路與裝置識別那就是IMSI號碼,手機的訊號也可以說是通過這個號碼在網路中傳遞的,並不是手機號碼。試想,你的SIM丟失後,補辦一張新的會換號碼嗎?是不會的.就是因為在你的手機號碼對應的IMSI號 在行動電信業者中被修改成新SIM卡的IMSI號碼。
那麼手機號為什麼有的就能顯示呢?
這個就像是一個變數,當行動電信業者為它賦值了,它自然就會有值。不賦值自然為空白。
對於移動的使用者,手機號碼(MDN)儲存在電訊廠商的伺服器中,而不是儲存在SIM卡裡。SIM卡只保留了IMSI和一些驗證資訊。手機每次入網註冊的時候,都會以簡訊的形式將IMSI及驗證資訊上傳到電訊廠商的伺服器,伺服器在完成註冊動作之後,會以簡訊的形式將註冊的結果下發到手機裡。下發的內容會因條件不同而不同。
如果伺服器在下發的簡訊中,不包含手機的號碼,手機是無法取得電話號碼。如果簡訊中包含了號碼,手機才會將其緩衝,以備他用.此外,對於其他運行商的SIM卡或者UIM卡,MDN有可能儲存在UIM卡中。100%能夠取得本機號碼不太可能。
移動神州行,聯通的卡是可以取到的.動感地帶的取不到.別的卡還沒有試過.
能夠讀取SIM卡號的話應該有前提.那就是SIM卡已經寫入了本機號碼,不然是無法讀取的。
具體處理代碼:(程式在模擬器上無法實現,必須串連手機)
//建立電話管理
TelephonyManager tm = (TelephonyManager)
//與手機建立串連
activity.getSystemService(Context.TELEPHONY_SERVICE);
//擷取手機號碼
String phoneId = tm.getLine1Number();
//記得在manifest file中添加
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
************************************************************************************************************************************************************
http://www.eoeandroid.com/thread-91296-1-1.html
/*** 電信APN列表* @author wudongdong**/public class APNNET {public static String CTWAP="ctwap";public static String CTNET="ctnet";}/*** 電信APN列表* @author wudongdong**/public class APNNET {public static String CTWAP="ctwap";public static String CTNET="ctnet";}//獲得APN的類型
/*** 獲得APN類型* @author wudongdong**/ public class ApnUtil {private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn"); /*** get apntype* @param context* @return*/ public static String getApnType(Context context){String apntype="nomatch";Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);c.moveToFirst();String user=c.getString(c.getColumnIndex("user"));if(user.startsWith(APNNET.CTNET)){apntype=APNNET.CTNET;}else if(user.startsWith(APNNET.CTWAP)){apntype=APNNET.CTWAP;}return apntype;}} /*** 獲得APN類型* @author wudongdong**/public class ApnUtil {private static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");/*** get apntype* @param context* @return*/ public static String getApnType(Context context){String apntype="nomatch";Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);c.moveToFirst();String user=c.getString(c.getColumnIndex("user"));if(user.startsWith(APNNET.CTNET)){apntype=APNNET.CTNET;}else if(user.startsWith(APNNET.CTWAP)){apntype=APNNET.CTWAP;}return apntype;}}
獲得手機號碼的話可以傳IMSI碼到指定介面,介面地址不方便說。但可以透露一點,必須走CTWAP,這也是判斷APN類型的原因,發現很多應用如果APN是走代理的話就不能連網,那麼再介紹一下用APN設定網路的代理資訊。
Cursor c = context.getContentResolver().query(PREFERRED_APN_URI,null, null, null, null);c.moveToFirst();String proxy=c.getString(c.getColumnIndex("proxy")); if (!"".equals(proxy) && proxy!=null) {Properties prop = System.getProperties();System.getProperties().put("proxySet", "true");prop.setProperty("http.proxyHost", c.getString(c.getColumnIndex("proxy")));prop.setProperty("http.proxyPort", c.getString(c.getColumnIndex("port")));String authentication = c.getString(c.getColumnIndex("user"))+ ":" + c.getString(c.getColumnIndex("password"));String encodedLogin = Base64.encode(authentication);uc.setRequestProperty("Proxy-Authorization", " BASIC "+ encodedLogin);} c.close();