我在布局文件中添加了两个发送按钮(mysend.xml):
我的java文件(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;
}
}
此代码在 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);
我的问题
如果我点击发送1按钮,则100%正常工作,并通过SIM1发送消息, 但如果我点击发送2按钮,则不会发送消息。
顺便说一句,抱歉我的英文不好答案 0 :(得分:2)
@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;
}
答案 1 :(得分:0)
对我来说,代码适用于sim1但是对于sim2没有任何反应。看着日志Nullpointer异常被抛出。所以问题在于name = isms2
。对于我的手机(芯片组展讯),对于sim1,它是isms0,对于sim2,它是isms1。您可以在dumpsys文件中查看自己的文件,或者在adb shell中使用以下代码进行检查:
adb shell service list | grep isms
这将列出您设备中可用的主题。对于我的设备,以下是输出:
8 isms: [com.android.internal.telephony.ISms]
12 isms1: [com.android.internal.telephony.ISms]
16 isms0: [com.android.internal.telephony.ISms]
请查看我的回答here。