今天就跟大家聊聊有關(guān)在Android中獲取本機(jī)號(hào)碼的方法有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Android獲取手機(jī)本機(jī)號(hào)碼的實(shí)現(xiàn)方法
反射TelephoneManager 獲取本機(jī)號(hào)碼,注意一下提供的接口有的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;
}看完上述內(nèi)容,你們對(duì)在Android中獲取本機(jī)號(hào)碼的方法有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章題目:在Android中獲取本機(jī)號(hào)碼的方法有哪些-創(chuàng)新互聯(lián)
標(biāo)題URL:http://chinadenli.net/article2/dspiic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、響應(yīng)式網(wǎng)站、微信公眾號(hào)、ChatGPT、面包屑導(dǎo)航、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容