国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android獲取手機本機號碼的實現(xiàn)方法

2019-12-12 01:56:09
字體:
供稿:網(wǎng)友

Android獲取手機本機號碼的實現(xiàn)方法

反射TelephoneManager 獲取本機號碼,注意一下提供的接口有的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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 张家港市| 烟台市| 西充县| 鄂尔多斯市| 克拉玛依市| 湘乡市| 广灵县| 慈溪市| 博客| 略阳县| 永胜县| 安陆市| 济南市| 阿拉善盟| 尉氏县| 溧水县| 卫辉市| 白银市| 沾化县| 保山市| 乌恰县| 普格县| 布拖县| 庆城县| 东莞市| 谷城县| 庆安县| 溧阳市| 平遥县| 胶州市| 宁陕县| 丰镇市| 岳阳市| 科技| 长丰县| 英德市| 恩施市| 淮阳县| 上高县| 海伦市| 安吉县|