SmsManager: 从 BroadcastReceiver 获取消息收件人 phone 号码

SmsManager: get message recipient phone number from BroadcastReceiver

我如何向多个收件人发送消息:

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);
}

此代码为每个用户的 phone 号码调用 SmsManager.sendTextMessage(...)

BroadcastReceiver 接收每条发送的消息并通知订阅者。我想在 BroadcastReceiver.onReceive 中获得 phone 个 SMS 收件人的数量,以通过 SendingProgress 传递它。

有什么办法吗? 我应该同步发送多条短信吗?

Is there any way to do this?

第 1 步:将您想要的信息放在 unequalSmsSentAction Intent

的额外部分

第 2 步:为每个 PendingIntent 使用唯一 ID(getBroadcast() 调用的第二个参数,其中 0 -- 将其替换为不同的值每条短信)

第 3 步:阅读发送到您的 BroadcastReceiver

Intent 中的额外信息

但是,任何应用程序都可以收听您的 unequalSmsSentAction 广播。理想情况下,将 Intent 中 extra 的值设置为 可以用来查找 phone 数字的东西,但不能被任何其他应用程序使用接收这些广播。

Should I send multiple SMSes synchronously?

据我所知,你不能同步发送它们。