Android获取手机本机号码的实现方法

更新时间:2023-07-05 19:29:16 阅读: 评论:0

Android获取⼿机本机号码的实现⽅法
Android获取⼿机本机号码的实现⽅法
反射TelephoneManager 获取本机号码,注意⼀下提供的接⼝有的SIM卡没写是获取不到的,该接⼝只适配Android5.0以上版本
public String getMsisdn(int slotId) {
return getLine1NumberForSubscriber(getSubIdForSlotId(slotId));
}
权限
<us-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) SystemService(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);
emirate
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("lephony.SubscriptionManager").getDeclaredMethod("getSubId", new Class[]{Integer.TYPE});
无边丝雨细如愁declaredMethod.tAccessible(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 = Class().getMethod("getLine1NumberForSubscriber", int.class);
method.tAccessible(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;
波俄战争}
如有疑问请留⾔或者到本站社区交流讨论,感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!

本文发布于:2023-07-05 19:29:16,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1080373.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:获取   机号码   机本   阅读   感谢   讨论   交流
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图