我被困了很长时间但无法解决。请帮助...我试图从Android设备以编程方式发送短信。我可以从许多设备成功发送,但不能使用android api level> = 22的三星双卡双待设备。对于api级别> = 22,我编码了,
public static final String SMS_SENT_ACTION = "package.name.app.SMS_SENT_ACTION";
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
String message = null;
switch (getResultCode()) {
case Activity.RESULT_OK:
message = "Message Sent Successfully !";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message = "Error.";
sendDefaultMessage(default_number, default_send_number, text);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message = "Error: No service.";
sendDefaultMessage(default_number, default_send_number, text);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message = "Error: Null PDU.";
sendDefaultMessage(default_number, default_send_number, text);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message = "Error: Radio off.";
sendDefaultMessage(default_number, default_send_number, text);
break;
}
Toast.makeText(GetAppointment.this, message, Toast.LENGTH_LONG).show();
}
}, new IntentFilter(SMS_SENT_ACTION));
SmsManager.getSmsManagerForSubscriptionId(subscription_id).sendTextMessage(send_number, null, messageToSend, PendingIntent.getBroadcast(
this, 0, new Intent(SMS_SENT_ACTION), 0), null);
没有与权限相关的问题,因为sms可以从asus双卡设备和sony单一SIM卡设备发送,api级别> = 22。
如何让它适用于所有设备?我非常需要帮助。
答案 0 :(得分:0)
您需要在sendTextMessage()
作为第二个参数提供要发送短信的SIM卡的服务中心号码,
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+9198********", msg, null, null);
答案 1 :(得分:0)
然后使用以下功能检查SIM卡设置:
public int getDefaultSimmm(Context context) {
Object tm = context.getSystemService(Context.TELEPHONY_SERVICE);
Method method_getDefaultSim;
int defaultSimm = -1;
try {
method_getDefaultSim = tm.getClass().getDeclaredMethod("getDefaultSim");
method_getDefaultSim.setAccessible(true);
defaultSimm = (Integer) method_getDefaultSim.invoke(tm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method method_getSmsDefaultSim;
int smsDefaultSim = -1;
try {
method_getSmsDefaultSim = tm.getClass().getDeclaredMethod("getSmsDefaultSim");
smsDefaultSim = (Integer) method_getSmsDefaultSim.invoke(tm);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return smsDefaultSim;
}
然后关注函数发送短信:
public boolean sendSMS(Context ctx, int simID, String toNum, String centerNum, String smsText, PendingIntent sentIntent, PendingIntent deliveryIntent) {
String name;
try {
if (simID == 0) {
name = "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 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, toNum, centerNum, smsText, 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(), toNum, centerNum, smsText, 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;
}
我希望它能帮到你