我正在尝试发送多条SMS消息。
我的代码:

for (Entry<String, String> entry : book.entrySet()) {
    sendSMS(entry.getValue(), message);
}


发送短信:

private void sendSMS(final String phoneNumber, String message) {
        String SENT = "SMS_SENT";
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    System.out.println("sent");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    System.out.println("No network "+ phoneNumber);
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;
                }
            }
        }, new IntentFilter(SENT));
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
    }


但是案件被执行了几次。对于4个条件:


无网路123456879
无网路123456879
无网路+79206501013
无网路123456879
无网路+79206501013
无网路+79206501012
无网路123456879
无网路+79206501013
无网路+79206501012
无网路987654321


可能是什么原因?谢谢!

最佳答案

我认为将registerReceiver(...)移出sendSMS将解决此问题。这似乎是多次注册的情况。

试试这个

Intent smsSentIntent = new Intent(SENT);
smsSentIntent.setClass(context, SmsSentBroadcastReceiver.class);

关于android - SmsManager执行了几次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21297184/

10-12 04:18