I'm put two send buttons in my layout file (mysend.xml):
- SEND with SIM1 (android:id="@+id/send1")
- SEND with SIM2 (android:id="@+id/send2")
My code in my java file (MySend.java)
public class MySend extends Activity {
public static boolean sendSMS(Context ctx, int simID, String nomor, String centerNum, String pesan, PendingIntent sentIntent, PendingIntent deliveryIntent) {
String name;
try {
if (simID == 0) {
name = "isms1";
// for model : "Philips T939" name = "isms0"
} else if (simID == 1) {
name = "isms2";
} 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("sendText", String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
method.invoke(stubObj, nomor, centerNum, pesan, sentIntent, deliveryIntent);
} else {
method = stubObj.getClass().getMethod("sendText", String.class, String.class, String.class, String.class, PendingIntent.class, PendingIntent.class);
method.invoke(stubObj, ctx.getPackageName(), nomor, centerNum, pesan, sentIntent, deliveryIntent);
}
return true;
} catch (ClassNotFoundException e) {
Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
} catch (NoSuchMethodException e) {
Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
} catch (InvocationTargetException e) {
Log.e("apipas", "InvocationTargetException:" + e.getMessage());
} catch (IllegalAccessException e) {
Log.e("apipas", "IllegalAccessException:" + e.getMessage());
} catch (Exception e) {
Log.e("apipas", "Exception:" + e.getMessage());
}
return false;
}
}
and this code inside public void onCreate(final Bundle savedInstanceState) {
// for send1 button
MySend.sendSMS(MySend.this,0,nomor,null,pesan,null,null);
// for send2 button
MySend.sendSMS(MySend.this,1,nomor,null,pesan,null,null);
My Problem
If i'm click on send1 button, it's 100% work and sent message with SIM1,
but if i'm click on send2 button, the message won't sent.
btw,sorry for my bad english
@SuppressLint("NewApi")
public static boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
String name;
try {
System.out.println("MODEL:"+Build.MODEL);
if (simID == 0) {
name = Build.MODEL.equals("Philips T939") ? "isms0" : "isms"; //
// for model : "Philips T939" name = "isms0"
}
else if (simID == 1) {
name = "isms2";
}
else {
throw new Exception("can not get service which for sim '" + simID + "', only 0,1 accepted as values");
}
Method declaredMethod = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", new Class[]{String.class});
declaredMethod.setAccessible(true);
Object invoke = declaredMethod.invoke(null, new Object[]{name});
declaredMethod = Class.forName("com.android.internal.telephony.ISms$Stub").getDeclaredMethod("asInterface", new Class[]{IBinder.class});
declaredMethod.setAccessible(true);
invoke = declaredMethod.invoke(null, new Object[]{invoke});
Log.e("SimUtil", "send msg - " + smsText);
if (Build.VERSION.SDK_INT < 18) {
invoke.getClass().getMethod("sendText", new Class[]{String.class, String.class, String.class, PendingIntent.class, PendingIntent.class})
.invoke(invoke, new Object[]{toNum, centerNum, smsText, sentIntent, deliveryIntent});
} else if (SimUtil.getLolli(ctx)) {
ArrayList arrayList = new ArrayList();
for (SubscriptionInfo subscriptionId : SubscriptionManager.from(ctx).getActiveSubscriptionInfoList()) {
int subscriptionId2 = subscriptionId.getSubscriptionId();
arrayList.add(Integer.valueOf(subscriptionId2));
Log.e("SimUtil", "SmsManager - subscriptionId: " + subscriptionId2);
}
SmsManager.getSmsManagerForSubscriptionId(((Integer) arrayList.get(simID)).intValue())
.sendTextMessage(toNum, null, smsText, sentIntent, deliveryIntent);
} else {
SmsManager.getDefault().sendTextMessage(toNum, null, smsText, sentIntent, deliveryIntent);
}
return true;
} catch (ClassNotFoundException e) {
Log.e("apipas", "ClassNotFoundException:" + e.getMessage());
e.printStackTrace();
Log.e("SimUtil", "error 1");
return false;
} catch (NoSuchMethodException e) {
Log.e("apipas", "NoSuchMethodException:" + e.getMessage());
Log.e("SimUtil", "error 2");
e.printStackTrace();
return false;
} catch (InvocationTargetException e) {
Log.e("apipas", "InvocationTargetException:" + e.getMessage());
Log.e("SimUtil", "error 3");
e.printStackTrace();
return false;
} catch (IllegalAccessException e) {
Log.e("apipas", "IllegalAccessException:" + e.getMessage());
Log.e("SimUtil", "error 4");
e.printStackTrace();
return false;
} catch (Exception e) {
Log.e("apipas", "Exception:" + e.getMessage());
Log.e("SimUtil", "error 5");
e.printStackTrace();
return false;
}
}
private static boolean lollypopPlus(Context context) {
if (Build.VERSION.SDK_INT < 22) {
return false;
}
if (SubscriptionManager.from(context).getActiveSubscriptionInfoCount() > 1) {
return true;
}
return false;
}
For me that code worked fine for sim1 but for sim2 nothing happened. Looking at the log Nullpointer exception was being thrown. So the problem was with name = isms2
. For my phone (chipset Spreadtrum), for sim1 its was isms0 and for sim2 it was isms1. You can check yours in the dumpsys file, or with the following code in adb shell:
adb shell service list | grep isms
This will list the isms available in your device. For my device following was the output:
8 isms: [com.android.internal.telephony.ISms]
12 isms1: [com.android.internal.telephony.ISms]
16 isms0: [com.android.internal.telephony.ISms]
Please view my answer here.