【发布时间】:2016-06-13 02:22:40
【问题描述】:
我如何向多个收件人发送消息:
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 中获取短信收件人的电话号码,以便通过SendingProgress 传递它。
有没有办法做到这一点? 我应该同时发送多条短信吗?
【问题讨论】:
标签: android rx-java rx-android smsmanager