Android獲取雙卡雙待手機(jī)的SIM卡信息示例代碼
前言
需要驗證手機(jī)號的功能,但是國內(nèi)的手機(jī)多是雙卡雙待的,無法獲取到兩個號碼。在Android的官方文檔是沒有提供相應(yīng)的Api的,因為標(biāo)準(zhǔn)的Andoird是沒有雙卡的,好像也只有國內(nèi)才會搞雙卡雙待的神器吧。
以下記錄一下做這個功能所學(xué)習(xí)到的東西。
Android 獲取本機(jī)手機(jī)號(適用于雙卡雙待手機(jī))
直接上代碼:
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; import android.telephony.CellInfo; import android.telephony.TelephonyManager; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private TextView tv2; // /////////////////////////////////// static String ISDOUBLE; static String SIMCARD; static String SIMCARD_1; static String SIMCARD_2; static boolean isDouble; // ////////////////////////////////// @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.text); tv2 = (TextView) findViewById(R.id.text2); tv2.setText("不知道哪個卡可用!"); getNumber(); } private void getNumber() { TelephonyManager tm = (TelephonyManager) this.getSystemService(this.TELEPHONY_SERVICE); String phoneNumber1 = tm.getLine1Number(); // String phoneNumber2 = tm.getGroupIdLevel1(); initIsDoubleTelephone(this); if (isDouble) { // tv.setText("這是雙卡手機(jī)!"); tv.setText("本機(jī)號碼是:" + " " + phoneNumber1 + " " + "這是雙卡手機(jī)!"); } else { // tv.setText("這是單卡手機(jī)"); tv.setText("本機(jī)號碼是:" + " " + phoneNumber1 + " " + "這是單卡手機(jī)"); } } public void initIsDoubleTelephone(Context context) { isDouble = true; Method method = null; Object result_0 = null; Object result_1 = null; TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); try { // 只要在反射getSimStateGemini 這個函數(shù)時報了錯就是單卡手機(jī)(這是我自己的經(jīng)驗,不一定全正確) method = TelephonyManager.class.getMethod("getSimStateGemini", new Class[] { int.class }); // 獲取SIM卡1 result_0 = method.invoke(tm, new Object[] { new Integer(0) }); // 獲取SIM卡2 result_1 = method.invoke(tm, new Object[] { new Integer(1) }); } catch (SecurityException e) { isDouble = false; e.printStackTrace(); // System.out.println("1_ISSINGLETELEPHONE:"+e.toString()); } catch (NoSuchMethodException e) { isDouble = false; e.printStackTrace(); // System.out.println("2_ISSINGLETELEPHONE:"+e.toString()); } catch (IllegalArgumentException e) { isDouble = false; e.printStackTrace(); } catch (IllegalAccessException e) { isDouble = false; e.printStackTrace(); } catch (InvocationTargetException e) { isDouble = false; e.printStackTrace(); } catch (Exception e) { isDouble = false; e.printStackTrace(); // System.out.println("3_ISSINGLETELEPHONE:"+e.toString()); } SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); Editor editor = sp.edit(); if (isDouble) { // 保存為雙卡手機(jī) editor.putBoolean(ISDOUBLE, true); // 保存雙卡是否可用 // 如下判斷哪個卡可用.雙卡都可以用 if (result_0.toString().equals("5") && result_1.toString().equals("5")) { if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "0"); } editor.putBoolean(SIMCARD_1, true); editor.putBoolean(SIMCARD_2, true); tv2.setText("雙卡可用"); } else if (!result_0.toString().equals("5") && result_1.toString().equals("5")) {// 卡二可用 if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "1"); } editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, true); tv2.setText("卡二可用"); } else if (result_0.toString().equals("5") && !result_1.toString().equals("5")) {// 卡一可用 if (!sp.getString(SIMCARD, "2").equals("0") && !sp.getString(SIMCARD, "2").equals("1")) { editor.putString(SIMCARD, "0"); } editor.putBoolean(SIMCARD_1, true); editor.putBoolean(SIMCARD_2, false); tv2.setText("卡一可用"); } else {// 兩個卡都不可用(飛行模式會出現(xiàn)這種種情況) editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, false); tv2.setText("飛行模式"); } } else { // 保存為單卡手機(jī) editor.putString(SIMCARD, "0"); editor.putBoolean(ISDOUBLE, false); } editor.commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
當(dāng)然不要忘記添加權(quán)限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
獲取雙卡雙待手機(jī)SIM卡信息
使用反射遍歷 TelephonyManager 中的方法,通過肉眼基本能找到獲取雙卡雙待號碼的方法,最后通過反射取到 SIM 卡信息。
// 遍歷 TelephonyManager 里的方法 public void printTelephonyManagerMethodNamesForThisDevice() { TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Class<?> telephonyClass; try { telephonyClass = Class.forName(telephony.getClass().getName()); Method[] methods = telephonyClass.getMethods(); for (int i = 0; i < methods.length; i++) { Log.i(TAG, "\n" + methods[i] + " declared by " + methods[i].getDeclaringClass()); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 獲取雙卡雙待 SIM 卡序列號 public void getSubscriberId() { TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Class<?> telephonyClass; Object result = null; Object result0 = null; Object result1 = null; try { telephonyClass = Class.forName(telephony.getClass().getName()); Method m1 = telephonyClass.getMethod("getSubscriberId"); Method m2 = telephonyClass.getMethod("getSubscriberId", new Class[]{int.class}); result = m1.invoke(telephony); result0 = m2.invoke(telephony, 0); result1 = m2.invoke(telephony, 1); } catch (Exception e) { e.printStackTrace(); } Log.i(TAG, " getSubscriberId : " + telephony.getSubscriberId() + "\n" + " result : " + result + "\n" + " result0 : " + result0 + "\n" + " result1 : " + result1 + "\n"); }
是否能取到手機(jī)號,取決于手機(jī)卡,而大部分手機(jī)卡都取不到手機(jī)號碼,只能取到 SIM 卡序列號。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android Drawerlayout側(cè)拉欄事件傳遞問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Drawerlayout側(cè)拉欄事件傳遞問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Ubuntu 14.04下創(chuàng)建Genymotion安卓虛擬機(jī)的步驟詳解
Android 模擬器一直以速度奇慢無比著稱,基本慢到不可用。本文介紹我一直在用的 Genymotion,速度不亞于真機(jī)。而且功能齊全,使用簡單。下面這篇文章主要介紹了Ubuntu 14.04下創(chuàng)建Genymotion虛擬機(jī)的步驟,需要的朋友可以參考下。2017-03-03Android?如何獲取傳感器的數(shù)據(jù)方法詳解
這篇文章主要介紹了Android?如何獲取傳感器的數(shù)據(jù),傳感器?Sensor?是一種檢測裝置,能感受到被測量的信息,并能將感受到的信息,按一定規(guī)律變換成為電信號或其他所需形式的信息輸出,以滿足信息的傳輸、處理、存儲、顯示、記錄和控制等要求2022-07-07Jetpack?Compose實現(xiàn)對角線滾動效果
這篇文章主要為大家詳細(xì)介紹了如何利用Jetpack?Compose實現(xiàn)一個簡單的對角線滾動效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02Android自定義StepView仿外賣配送進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義StepView仿外賣配送進(jìn)度,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05