对于您发送的每条 SMS(或部分 SMS),您都需要提供一个 PendingIntent。在该 PendingIntent 中,您放置了一个 Intent,当 SMS(或部分)成功发送或未发送时,您将收到该 Intent。在这个 Intent 中,您可以使用 extras 放置额外的信息。因此,例如,在发送消息时,代码可能如下所示...
String receiverCodeForThisMessage = "STRING_CODE_FOR_MY_SMS_OUTCOME_RECEIVER";
int uniqueCodeForThisPartOfThisSMS = 100*numberSMSsentSoFar+PartNumberOfThisSMSpart;
Intent intent = new Intent(receiverCodeForThisMessage);
intent.putExtra("TagIdentifyIngPieceOfInformationOne", piece1);
intent.putExtra("TagIdentifyIngPieceOfInformationTwo", piece2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueCodeForThisPartOfThisSMS, intent, 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("phonenumber", "Fred Bloggs", "Hello!", pendingIntent, null);
但在此之前,您将注册一个接收器,该接收器将获得您设置的结果和意图;从那个意图中你提取信息片段:
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// Get information about this message
int piece1 = intent.getIntExtra("TagIdentifyIngPieceOfInformationOne", -1);
int piece2 = intent.getIntExtra("TagIdentifyIngPieceOfInformationTwo", -1);
if (getResultCode() == Activity.RESULT_OK) {
// success code
}
else {
// failure code
}
}, new IntentFilter(receiverCodeForThisMessage));