Android基于APN獲取手機號的方法
本文實例講述了Android基于APN獲取手機號的方法。分享給大家供大家參考。具體如下:
之前很多人說無法完全獲取手機號,是因為現(xiàn)在有的卡不能獲取,有的卡能獲取,現(xiàn)在我們可以換一種思路來考慮問題,就是用APN的方式。請看代碼:
APNNET.java如下:
/** * 電信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; } }
Java代碼如下:
/** 獲得手機號碼的話可以傳IMSI碼到指定接口,接口地址不方便說。但可以透露一點,必須走CTWAP,這也是判斷APN類型的原因,發(fā)現(xiàn)很多應(yīng)用如果APN是走代理的話就不能聯(lián)網(wǎng),那么再介紹一下用APN設(shè)置網(wǎng)絡(luò)的代理信息。 */ 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();
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android中fragment與activity之間的交互(兩種實現(xiàn)方式)
本篇文章主要介紹了Android中fragment與activity之間的交互(兩種實現(xiàn)方式),相信對大家學習會有很好的幫助,需要的朋友一起來看下吧2016-12-12OKhttp攔截器實現(xiàn)實踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實現(xiàn)實踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Android Studio開發(fā)中Gradle各種常見報錯問題解決方案
這篇文章主要為大家介紹了Android Studio開發(fā)中Gradle各種常見報錯問題解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路
用戶退出應(yīng)用前給出一個提示是很有必要的,因為可能是用戶并不真的想退出,而只是一不小心按下了返回鍵,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個Dialog提示框;個人覺得再按一次返回鍵退出程序很有必要,接下來介紹一些簡單實現(xiàn)2013-01-01android上的一個網(wǎng)絡(luò)接口和圖片緩存框架enif簡析
android上的一個網(wǎng)絡(luò)接口和圖片緩存框架enif詳細介紹:底層網(wǎng)絡(luò)接口采用apache的httpclient連接池框架、圖片緩存采用基于LRU的算法等等,需要了解的朋友可以詳細參考下2012-12-12android 自定義ScrollView實現(xiàn)背景圖片伸縮的實現(xiàn)代碼及思路
本文純屬個人見解,是對前面學習的總結(jié),如有描述不正確的地方還請高手指正~,首先還是按照通例給大家看下示例.2013-05-05