Android獲取手機(jī)本機(jī)號碼的實(shí)現(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組件之BroadcastReceiver廣播接收者
這篇文章主要為大家介紹了Android組件之BroadcastReceiver廣播接收者實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果
大家好,本篇文章主要講的是Android中FlowLayout組件實(shí)現(xiàn)瀑布流效果,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android 4.4以上"沉浸式"狀態(tài)欄效果的實(shí)現(xiàn)方法
Android與ios效果互仿早已不是什么稀奇的事,我猜大概這個(gè)效果來自ios吧,有爭議說這種效果不能叫做沉浸式,叫透明狀態(tài)欄更合適,我也感覺這和沉浸式的含義不太一致。但是大家都這么叫了,那就這樣唄。下面來一起看看關(guān)于Android 4.4以上"沉浸式"效果的實(shí)現(xiàn)方法。2016-09-09
Android RecyclerView 數(shù)據(jù)綁定實(shí)例代碼
本文主要介紹Android RecyclerView 數(shù)據(jù)綁定的資料,這里詳細(xì)說明如何實(shí)現(xiàn) Android RecyclerView的數(shù)據(jù)綁定,并附示例代碼,有需要的小伙伴可以參考下2016-09-09
Android編程實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)AIDL(跨進(jìn)程通信)的原理、具體流程與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06
深入android Unable to resolve target ''android-XX''詳解
本篇文章是對android Unable to resolve target 'android-XX'錯(cuò)誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android App中實(shí)現(xiàn)相冊瀑布流展示的實(shí)例分享
這篇文章主要介紹了Android App中實(shí)現(xiàn)相冊瀑布流展示的實(shí)例分享,例子中利用到了緩存LruCache類的相關(guān)算法來解決大量加載問題,需要的朋友可以參考下2016-04-04

