当我尝试使用SMS管理器从应用程序发送来自双SIM卡设备的短信时,它会从SIM卡发送短信。你能帮我解决这个问题。
private void sendSMS(String phoneNumber)
{
String msg="app Test";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, msg, null, null);
// this.stopSelf();
Toast.makeText(CallBlockingService.this, "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(CallBlockingService.this,
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
答案 0 :(得分:0)
如果您可以将此代码用于API级别22+。这里22-我正在使用默认的短信频道。
private void sendDirectSMS() {
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();
}
}
答案 1 :(得分:-4)
提供您要发送短信的SIM卡的服务中心号码。我的SIM服务中心号码是+919892051914,另一个是+919022000500
因此,如果我想从我的SIM卡发送短信,那么代码将是
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+919892051914", msg, null, null);
就是这样......享受....