时间:2020-12-17 02:22:36 | 栏目:Android代码 | 点击:次
前言
需要验证手机号的功能,但是国内的手机多是双卡双待的,无法获取到两个号码。在Android的官方文档是没有提供相应的Api的,因为标准的Andoird是没有双卡的,好像也只有国内才会搞双卡双待的神器吧。
以下记录一下做这个功能所学习到的东西。
Android 获取本机手机号(适用于双卡双待手机)
直接上代码:
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("这是双卡手机!"); tv.setText("本机号码是:" + " " + phoneNumber1 + " " + "这是双卡手机!"); } else { // tv.setText("这是单卡手机"); tv.setText("本机号码是:" + " " + phoneNumber1 + " " + "这是单卡手机"); } } 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 这个函数时报了错就是单卡手机(这是我自己的经验,不一定全正确) 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) { // 保存为双卡手机 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 {// 两个卡都不可用(飞行模式会出现这种种情况) editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, false); tv2.setText("飞行模式"); } } else { // 保存为单卡手机 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; } }
当然不要忘记添加权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
获取双卡双待手机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"); }
是否能取到手机号,取决于手机卡,而大部分手机卡都取不到手机号码,只能取到 SIM 卡序列号。
总结