【问题标题】:SmsManager: get message recipient phone number from BroadcastReceiverSmsManager:从 BroadcastReceiver 获取消息接收者电话号码
【发布时间】: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


    【解决方案1】:

    有没有办法做到这一点?

    第 1 步:将您想要的信息放入您的 unequalSmsSentAction Intent 的额外部分中

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

    第 3 步:阅读发送至您的BroadcastReceiverIntent 中的额外信息

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

    我应该同时发送多条短信吗?

    AFAIK,你不能同步发送它们。

    【讨论】:

    • 按“同时”我依次理解。例如:只有在上一个成功之后才发送消息。但是经过你的解释,就不需要了。谢谢!
    • 同步* @CommonsWare 为什么我应该为每个 PendingIntent 使用唯一的 id(requestCode)?
    • @Alexandr:否则,每次拨打getBroadcast() 时,您只会得到相同的PendingIntent。您的 N 条并发消息需要唯一的 PendingIntents,因为您有 N 条附加信息(每个 Intent 一条)。 Extras 本身不计入唯一性,否则您的所有 Intent 对象将是相同的。使用请求代码(getBroadcast() 的第二个参数)是获得唯一 PendingIntents 的最简单方法。
    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多