温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Android中获取本机号码的方法有哪些

发布时间:2021-01-28 10:42:21 来源:亿速云 阅读:598 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关在Android中获取本机号码的方法有哪些,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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

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

 public String getMsisdn(int slotId) {
    return getLine1NumberForSubscriber(getSubIdForSlotId(slotId));
  }

  权限

<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; 
}

看完上述内容,你们对在Android中获取本机号码的方法有哪些有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI