如何在 Dual SIM mobile Moto E(android 版本 4.4.4)中使用 java 反射发送短信?
How to send a SMS using java reflection in Dual SIM mobile Moto E ( android version 4.4.4)?
我已经尝试使用此 link How to send a SMS using SMSmanager in Dual SIM mobile? 中提到的代码
我使用的 simID 名称是 "isms_msim".
try {
if (simID == 0) {
name = "isms";
} else if (simID == 1) {
name = "isms_msim";
} else {
throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
}
Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
method.setAccessible(true);
Object param = method.invoke(null, name);
method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", IBinder.class);
method.setAccessible(true);
Object stubObj = method.invoke(null, param);
if (Build.VERSION.SDK_INT < 18) {
method = stubObj.getClass().getMethod("sendMultipartText", String.class, String.class, List.class, List.class, List.class);
method.invoke(stubObj, toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
} else {
method = stubObj.getClass().getMethod("sendMultipartText", String.class, String.class, String.class, List.class, List.class, List.class);
===> method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
}
return true;
} catch (ClassNotFoundException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (NoSuchMethodException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (InvocationTargetException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (IllegalAccessException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (Exception e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
}
但是此代码在 else 块中以 ===> 开头的行给出以下异常:
SecurityException:对不正确接口的 Binder 调用
有什么想法吗?
我花了一些时间自己得到了解决方案。
通过 java 反射调用 https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/android/telephony/MSimSmsManager.java and https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/android/telephony/MSimTelephonyManager.java 文件中的 API 以获取订阅信息并发送短信。
我已经尝试使用此 link How to send a SMS using SMSmanager in Dual SIM mobile? 中提到的代码 我使用的 simID 名称是 "isms_msim".
try {
if (simID == 0) {
name = "isms";
} else if (simID == 1) {
name = "isms_msim";
} else {
throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
}
Method method = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
method.setAccessible(true);
Object param = method.invoke(null, name);
method = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", IBinder.class);
method.setAccessible(true);
Object stubObj = method.invoke(null, param);
if (Build.VERSION.SDK_INT < 18) {
method = stubObj.getClass().getMethod("sendMultipartText", String.class, String.class, List.class, List.class, List.class);
method.invoke(stubObj, toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
} else {
method = stubObj.getClass().getMethod("sendMultipartText", String.class, String.class, String.class, List.class, List.class, List.class);
===> method.invoke(stubObj, ctx.getPackageName(), toNum, centerNum, smsTextlist, sentIntentList, deliveryIntentList);
}
return true;
} catch (ClassNotFoundException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (NoSuchMethodException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (InvocationTargetException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (IllegalAccessException e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
} catch (Exception e) {
Logger.Log(TAG, Logger.LogLevel.ERROR, "Error while sending multiple sms, stack trace: " + e.getStackTrace());
}
但是此代码在 else 块中以 ===> 开头的行给出以下异常: SecurityException:对不正确接口的 Binder 调用
有什么想法吗?
我花了一些时间自己得到了解决方案。 通过 java 反射调用 https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/android/telephony/MSimSmsManager.java and https://github.com/gp-b2g/frameworks_base/blob/master/telephony/java/android/telephony/MSimTelephonyManager.java 文件中的 API 以获取订阅信息并发送短信。