SMS状态BroadcastReceiver多次调用

时间:2014-10-15 07:34:39

标签: sms status

为什么我的广播接收器多次通话?下面的代码运行良好,但删除方法被多次调用。下面的方法是从AWS SQS发送消息并在删除之前检测SMS状态..我想知道为什么Activity.RESULT_OK被多次广播......

void send(String msgbody, String msg_receipients, Intent intent,
        final Context context, final Message message,
        final AmazonSQSClient sqsClient, final String queueUrl) {

    String SENT = "SMS_SENT";
    Intent sent_ = new Intent(SENT);
    PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sent_, 0);
    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();

    context.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent intent) {

            if (getResultCode() == Activity.RESULT_OK) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Delete(message, sqsClient, queueUrl);
                    }
                }).start();
            } else {
                Log.d(TAG, "Message Failed. Error Code: " + getResultCode());
            }

        }
    }, new IntentFilter(SENT));

    SmsManager smsmgr = SmsManager.getDefault();
    final ArrayList<String> messages = smsmgr.divideMessage(msgbody);
    final int c = messages.size();

    try {
        Log.d(TAG, "Sending messages to: " + msg_receipients);

        for (int i = 0; i < c; i++) {
            final String m = messages.get(i);
            Log.d(TAG, "divided messages: " + m);
            sentPendingIntents.add(i, sentPI);
        }

        smsmgr.sendMultipartTextMessage(msg_receipients, null, messages,
                sentPendingIntents, null);

    } catch (Exception e) {
        Log.e(TAG, "unexpected error", e);
    }

}

1 个答案:

答案 0 :(得分:5)

我通过改变注册接收器的方式以及在调用接收器之后取消注册它来解决这个问题。

void send(String msgbody, String msg_receipients, Intent intent,
        final Context context, final Message message,
        final AmazonSQSClient sqsClient, final String queueUrl) {

    String SENT = "SMS_SENT";
    Intent sent_ = new Intent(SENT);
    PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sent_, 0);
    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();

    BroadcastReceiver rcvr = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                context.unregisterReceiver(this);
                if (getResultCode() == Activity.RESULT_OK) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Delete(message, sqsClient, queueUrl);
                        }
                    }).start();
                } else {
                    Log.d(TAG, "Message Failed. Error Code: "
                            + getResultCode());
                }
            } catch (Exception x) {
                x.printStackTrace();
            }

        }
    };
    IntentFilter filter = new IntentFilter(SENT);
    context.registerReceiver(rcvr, filter);