欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年10月09日 17:09:14   作者:蒼老師s  
這篇文章主要介紹了Android獲取手機本機號碼的實現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠?qū)崿F(xiàn)這樣的方法,需要的朋友可以參考下

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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Android組件之BroadcastReceiver廣播接收者

    Android組件之BroadcastReceiver廣播接收者

    這篇文章主要為大家介紹了Android組件之BroadcastReceiver廣播接收者實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Android中FlowLayout組件實現(xiàn)瀑布流效果

    Android中FlowLayout組件實現(xiàn)瀑布流效果

    大家好,本篇文章主要講的是Android中FlowLayout組件實現(xiàn)瀑布流效果,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • android通用xml解析方法

    android通用xml解析方法

    之前做的網(wǎng)絡(luò)相關(guān)的應(yīng)用,里面涉及到了xml的解析,由于急于完成任務(wù)也沒有設(shè)計就直接敲代碼。這幾天給一個朋友做項目的時候也涉及到了xml的解析,而且解析的內(nèi)容比較多,我查看了以前的項目中的相關(guān)代碼,頓時覺得很多代碼都是可以優(yōu)化的。在此寫兩個通用的xml解析方法,當(dāng)然這里所講的通用只是在一定程度上并且需要遵守一些規(guī)范。
    2013-03-03
  • Android 4.4以上

    Android 4.4以上"沉浸式"狀態(tài)欄效果的實現(xiàn)方法

    Android與ios效果互仿早已不是什么稀奇的事,我猜大概這個效果來自ios吧,有爭議說這種效果不能叫做沉浸式,叫透明狀態(tài)欄更合適,我也感覺這和沉浸式的含義不太一致。但是大家都這么叫了,那就這樣唄。下面來一起看看關(guān)于Android 4.4以上"沉浸式"效果的實現(xiàn)方法。
    2016-09-09
  • Android RecyclerView 數(shù)據(jù)綁定實例代碼

    Android RecyclerView 數(shù)據(jù)綁定實例代碼

    本文主要介紹Android RecyclerView 數(shù)據(jù)綁定的資料,這里詳細說明如何實現(xiàn) Android RecyclerView的數(shù)據(jù)綁定,并附示例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • Android中獲取手機屏幕大小的方法

    Android中獲取手機屏幕大小的方法

    這篇文章主要介紹了Android中獲取手機屏幕大小的方法,Android開發(fā)需要獲得屏幕的寬高,本文為大家解析 Android中如何獲取手機屏幕大小,需要的朋友可以參考下
    2015-12-12
  • Android?TabLayout?自定義樣式及使用詳解

    Android?TabLayout?自定義樣式及使用詳解

    這篇文章主要為大家介紹了Android?TabLayout?自定義樣式及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android編程實現(xiàn)AIDL(跨進程通信)的方法詳解

    Android編程實現(xiàn)AIDL(跨進程通信)的方法詳解

    這篇文章主要介紹了Android編程實現(xiàn)AIDL(跨進程通信)的方法,結(jié)合實例形式詳細分析了Android實現(xiàn)AIDL(跨進程通信)的原理、具體流程與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2016-06-06
  • 深入android Unable to resolve target ''android-XX''詳解

    深入android Unable to resolve target ''android-XX''詳解

    本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • Android App中實現(xiàn)相冊瀑布流展示的實例分享

    Android App中實現(xiàn)相冊瀑布流展示的實例分享

    這篇文章主要介紹了Android App中實現(xiàn)相冊瀑布流展示的實例分享,例子中利用到了緩存LruCache類的相關(guān)算法來解決大量加載問題,需要的朋友可以參考下
    2016-04-04

最新評論