在双卡手机中从一张 SIM 卡发送短信
Send SMS from one SIM in dual sim moble phones
当我尝试通过使用短信管理器的应用程序从双 SIM android 设备发送短信时,它正在从双 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();
}
}
提供您要从中发送短信的 SIM 卡的服务中心号码。我的一个 SIM 卡服务中心号码是 +919892051914,另一个是 +919022000500
因此,如果我想从我的一张 SIM 卡发送短信,则代码将为
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+919892051914", msg, null, null);
就是这样...享受...
如果您可以将此代码用于 API 级别 22+。这里 22- 我正在使用默认的 SMS 频道。
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();
}
}
当我尝试通过使用短信管理器的应用程序从双 SIM android 设备发送短信时,它正在从双 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();
}
}
提供您要从中发送短信的 SIM 卡的服务中心号码。我的一个 SIM 卡服务中心号码是 +919892051914,另一个是 +919022000500
因此,如果我想从我的一张 SIM 卡发送短信,则代码将为
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, "+919892051914", msg, null, null);
就是这样...享受...
如果您可以将此代码用于 API 级别 22+。这里 22- 我正在使用默认的 SMS 频道。
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();
}
}