android 双卡手机发短信/判断手机是否为双卡
MTK和高通的都应该怎么识别?
这里困扰了很多的开发者,这一篇可能都是错的,希望能提供参考。
try { Class<?> smsManagerClass = null; Class[] divideMessagePamas = { String.class }; Class[] sendMultipartTextMessagePamas = { String.class,String.class, ArrayList.class, ArrayList.class,ArrayList.class, int.class }; Method divideMessage = null; Method sendMultipartTextMessage = null; smsManagerClass = Class.forName("android.telephony.SmsManager"); Method method = smsManagerClass.getMethod("getDefault", new Class[]{}); Object smsManager = method.invoke(smsManagerClass, new Object[]{}); divideMessage = smsManagerClass.getMethod("divideMessage",divideMessagePamas); sendMultipartTextMessage = smsManagerClass.getMethod("sendMultipartTextMessage", sendMultipartTextMessagePamas); ArrayList<String> magArray = (ArrayList<String>) divideMessage.invoke(smsManager, content); sendMultipartTextMessage.invoke(smsManager,phone, "", magArray, null, null,phoneType); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); }
通过反射发现sendMultipartTextMessage多了一个phoneType的参数,这个参数就是这只用GMS还是CDMA卡发送的。phoneType获取方法,iTelephony.getActivePhoneType();//1=GSM,2=CDMA
判断手机是否为双卡:
public static void initIsDoubleTelephone(Context context){ boolean 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卡1 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); } 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); } 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); } else {//两个卡都不可用(飞行模式会出现这种种情况) editor.putBoolean(SIMCARD_1, false); editor.putBoolean(SIMCARD_2, false); } }else{ //保存为单卡手机 editor.putString(SIMCARD, "0"); editor.putBoolean(ISDOUBLE, false); } editor.commit(); }
/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:278401545
*
*/