0
廣播接收器的生命週期和操作我也不清楚,我已經看了機器人幫助庫,但仍然是不明確的,請看下面的例子:動態創建的廣播接收器的生命週期
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自動取消註冊,還是需要手動完成?
我會感謝任何幫助,我希望我很清楚。
BTW 確認功能,發送確認到服務器(異步)和MI類是MessageInfo(手機,機身,ID)