1

BroadcastReceiver的生命周期和操作我不清楚,我已经看过android帮助库,但仍然不清楚,考虑下面的例子:

private void sendSMS(final MessageInfo MI) {

    String SENT = "SMS_SENT" ;

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    // ---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Confirm(MI);
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "שליחה נכשלה",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "שליחה נכשלה, אין רשת",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "שליחה נכשלה, מצב טיסה",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(SENT));

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(MI.Message());
    int numParts = parts.size();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
    for (int i = 0; i < numParts; i++) {
        sentIntents.add(sentPI);
    }
    sms.sendMultipartTextMessage(MI.Phone(), null, parts, sentIntents, null);

}
}

如果我发送大量消息怎么办,它会为每条消息创建一个 BroadcastReceiver 吗?当发送的确认返回时,是异步返回还是同步返回?BroadcastReceiver 会自动注销,还是需要手动完成?

我会很感激任何帮助,我希望我很清楚。

顺便说一句,Confirm 函数向服务器发送确认(异步),MI 类是 MessageInfo(phone,body,ID)

4

1 回答 1

3

如果我发送大量消息怎么办,它会为每条消息创建一个 BroadcastReceiver 吗?

是的。这不是一个好主意。请注册一次

当发送的确认返回时,是异步返回还是同步返回?

异步。

BroadcastReceiver 会自动注销吗

不。

是否需要手动完成?

是的,通过unregisterReceiver().

于 2013-04-14T14:03:54.040 回答