我如何向多个收件人发送消息:
public void send(List<User> participants,
Func1<User, String> messageTextCallback,
Subscriber<SendingProgress> subscriber) { // TODO: throw error
Observable<SendingProgress> observable = Observable.create(sub -> {
String unequalSmsSentAction = UUID.randomUUID().toString();
context.registerReceiver(new BroadcastReceiver() {
private int totalToSend = participants.size();
private int sentCounter = 0;
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
sub.onNext(
new SendingProgress(totalToSend, ++sentCounter)
);
if(sentCounter == totalToSend) {
sub.onCompleted();
context.unregisterReceiver(this);
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
subscriber.onError(new SendSmsException());
sub.onCompleted();
}
}
}, new IntentFilter(unequalSmsSentAction));
int totalToSend = participants.size();
for(int i = 0; i < totalToSend; i++) {
User participant = participants.get(i);
logger.d(TAG, "sending to: " + participant.getUsername());
PendingIntent sentPi = PendingIntent.getBroadcast(context, 0, new Intent(unequalSmsSentAction), 0);
smsManager.sendTextMessage(
participant.getPhones().get(0),
null,
messageTextCallback.call(participant),
sentPi,
null
);
}
});
observable
.subscribeOn(ioScheduler)
.observeOn(mainThreadScheduler)
.subscribe(subscriber);
}
此代码为每个用户的电话号码调用 SmsManager.sendTextMessage(...)
。
BroadcastReceiver
接收每条发送的消息并通知订阅者。我想在 BroadcastReceiver.onReceive
中获取 SMS 收件人的电话号码,以通过 SendingProgress
传递它。
有什么办法可以做到这一点吗? 我应该同步发送多条短信吗?
最佳答案
Is there any way to do this?
第 1 步:将您想要的信息放入 unequalSmsSentAction
Intent
第 2 步:为每个 PendingIntent
使用唯一 ID(getBroadcast()
调用的第二个参数,其中有 0
-- 用每个 SMS 的不同值替换它)
第 3 步:从传递给 BroadcastReceiver
的 Intent
中读取额外信息
但是,任何应用程序都可以监听您的 unequalSmsSentAction
广播。理想情况下,将 Intent
中的 extra 值设置为 您 可以用来查找电话号码的东西,但不能被接收这些广播的任何其他应用程序使用.
Should I send multiple SMSes synchronously?
据我所知,您不能同步发送它们。
关于android - SmsManager:从 BroadcastReceiver 获取消息接收者电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777585/