If you can use this code for API level 22+.
private void sendDirectSMS() {
private static String SENT = "SMS_SENT", DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
// SEND BroadcastReceiver
BroadcastReceiver sendSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
showSnackBar(getString(R.string.sms_sent));
Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_SUCCESS);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
showSnackBar(getString(R.string.sms_send_failed_try_again));
Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
showSnackBar(getString(R.string.no_service_sms_failed));
Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
showSnackBar(getString(R.string.no_service_sms_failed));
Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
showSnackBar(getString(R.string.no_service_sms_failed));
Analytics.track(AnalyticsEvents.SEND_REMINDER_SMS_APP_FAILED);
break;
}
}
};
// DELIVERY BroadcastReceiver
BroadcastReceiver deliverSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), R.string.sms_delivered,
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), R.string.sms_not_delivered,
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(sendSMS, new IntentFilter(SENT));
registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
String smsText = getSmsText();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManager localSubscriptionManager = SubscriptionManager.from(context);
if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(0);
SubscriptionInfo simInfo2 = (SubscriptionInfo) localList.get(1);
//SendSMS From SIM One
SmsManager.getSmsManagerForSubscriptionId(simInfo1.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);
//SendSMS From SIM Two
SmsManager.getSmsManagerForSubscriptionId(simInfo2.getSubscriptionId()).sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);
}
} else {
SmsManager.getDefault().sendTextMessage(customer.getMobile(), null, smsText, sentPI, deliveredPI);
Toast.makeText(getBaseContext(), R.string.sms_sending, Toast.LENGTH_SHORT).show();
}
}
Also add permission.
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…