android5.1本机号码,Android获取手机本机号码的实现方法

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

反射TelephoneManager 获取本机号码,注意一下提供的接口有的SIM卡没写是获取不到的,该接口只适配Android5.0以上版本

public String getMsisdn(int slotId) {

return getLine1NumberForSubscriber(getSubIdForSlotId(slotId));

}

权限

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;

}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取双卡 Android 手机的电话号码可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String sim1PhoneNumber = telephonyManager.getLine1Number(0); String sim2PhoneNumber = telephonyManager.getLine1Number(1); ``` 需要注意的是,有些运营商可能不会将电话号码存储在 SIM 卡中,因此可能无法获取到电话号码获取双卡 Android 手机的运营商频段可以使用下面的代码: ```java TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo(); for (CellInfo cellInfo : cellInfoList) { if (cellInfo instanceof CellInfoGsm) { CellIdentityGsm cellIdentityGsm = ((CellInfoGsm) cellInfo).getCellIdentity(); int mcc = cellIdentityGsm.getMcc(); int mnc = cellIdentityGsm.getMnc(); int arfcn = cellIdentityGsm.getArfcn(); // TODO: 处理 GSM 频段信息 } else if (cellInfo instanceof CellInfoCdma) { CellIdentityCdma cellIdentityCdma = ((CellInfoCdma) cellInfo).getCellIdentity(); int sid = cellIdentityCdma.getSystemId(); int nid = cellIdentityCdma.getNetworkId(); int bid = cellIdentityCdma.getBasestationId(); int freq = cellIdentityCdma.getFrequency(); // TODO: 处理 CDMA 频段信息 } else if (cellInfo instanceof CellInfoLte) { CellIdentityLte cellIdentityLte = ((CellInfoLte) cellInfo).getCellIdentity(); int mcc = cellIdentityLte.getMcc(); int mnc = cellIdentityLte.getMnc(); int earfcn = cellIdentityLte.getEarfcn(); // TODO: 处理 LTE 频段信息 } } ``` 需要注意的是,以上代码需要在 Android 5.1 及以上的版本上运行,并且需要在 AndroidManifest.xml 文件中添加相应的权限: ```xml <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值