欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-13 08:05:10
android获取手机本机号码的实现方法 反射telephonemanager 获取本机号码,注意一下提供的接口有的sim卡没写是获取不到的,该接口只适配android5...

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

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

上一篇:

下一篇: