Android獲取手機(jī)本機(jī)號碼的實(shí)現(xiàn)方法
更新時間:2017年10月09日 17:09:14 作者:蒼老師s
這篇文章主要介紹了Android獲取手機(jī)本機(jī)號碼的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠?qū)崿F(xiàn)這樣的方法,需要的朋友可以參考下
Android獲取手機(jī)本機(jī)號碼的實(shí)現(xiàn)方法
反射TelephoneManager 獲取本機(jī)號碼,注意一下提供的接口有的SIM卡沒寫是獲取不到的,該接口只適配Android5.0以上版本
public String getMsisdn(int slotId) {
return getLine1NumberForSubscriber(getSubIdForSlotId(slotId));
}
權(quán)限
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
public class RegisterMessage {
private static Context mContext;
private static TelephonyManager mTelephonyManager;
private ConnectivityManager mConnMngr;
private static SubscriptionManager mSubscriptionManager;
public RegisterMessage(Context context) {
mContext = context;
mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephonyManager == null) {
throw new Error("telephony manager is null");
}
mConnMngr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
mSubscriptionManager = SubscriptionManager.from(mContext);
}
public String getMsisdn(int slotId) {//slotId 0為卡1 ,1為卡2
return getLine1NumberForSubscriber(getSubIdForSlotId(slotId));
}
rivate int getSubIdForSlotId(int slotId) {
int[] subIds = getSubId(slotId);
if (subIds == null || subIds.length < 1 || subIds[0] < 0) {
return -1;
}
MLog.d("getSubIdForSlotId = "+subIds[0]);
return subIds[0];
}
private static int[] getSubId(int slotId) {
Method declaredMethod;
int[] subArr = null;
try {
declaredMethod = Class.forName("android.telephony.SubscriptionManager").getDeclaredMethod("getSubId", new Class[]{Integer.TYPE});
declaredMethod.setAccessible(true);
subArr = (int[]) declaredMethod.invoke(mSubscriptionManager,slotId);
} catch (ClassNotFoundException e) {
e.printStackTrace();
declaredMethod = null;
} catch (IllegalArgumentException e2) {
e2.printStackTrace();
declaredMethod = null;
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
declaredMethod = null;
} catch (ClassCastException e4) {
e4.printStackTrace();
declaredMethod = null;
} catch (IllegalAccessException e5){
e5.printStackTrace();
declaredMethod = null;
}catch (InvocationTargetException e6){
e6.printStackTrace();
declaredMethod = null;
}
if(declaredMethod == null) {
subArr = null;
}
MLog.d("getSubId = "+subArr[0]);
return subArr;
}
private String getLine1NumberForSubscriber(int subId){
Method method;
String status = null;
try {
method = mTelephonyManager.getClass().getMethod("getLine1NumberForSubscriber", int.class);
method.setAccessible(true);
status = String.valueOf(method.invoke(mTelephonyManager, subId));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
MLog.d("getLine1NumberForSubscriber = "+status);
return status;
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android RecyclerView的Item自定義動畫及DefaultItemAnimator源碼分析
這篇文章主要介紹了Android RecyclerView的Item自定義動畫及DefaultItemAnimator源碼,感興趣的小伙伴們可以參考一下2016-07-07
android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
WebView設(shè)置WebViewClient的方法
這篇文章主要介紹了 WebView設(shè)置WebViewClient的方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android動態(tài)修改應(yīng)用圖標(biāo)與名稱的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01

