如何知道Android BroadcastReceiver发送/发送了哪些短信?

时间:2013-04-05 13:19:41

标签: android sms broadcastreceiver smsmanager

嗨大家我已经制作了一个简单的应用程序,在Android上发送多条短信,我找不到已发送/已发送的短信,因为发送通知并不知道它是什么。

这是我的代码:

    this.destination = data[0];
    this.body = data[1];

    PendingIntent piSend = PendingIntent.getBroadcast(aCtx, 0, new Intent(Cons.SMS_SENT),0);
    PendingIntent piDelivered = PendingIntent.getBroadcast(aCtx, 0, new Intent(Cons.SMS_DELIVERED), 0);

    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(destination, null, body, piSend, piDelivered);

然后我使用广播接收器来获取传送状态

public class DeliverSMSBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    switch (getResultCode())
    {
        case Activity.RESULT_OK:
            Toast.makeText(context, "SMS delivered",
                    Toast.LENGTH_SHORT).show();
            Log.d(Cons.TAGS+" RK","RESULT_OK=> DELIVER");
            break;
        case Activity.RESULT_CANCELED:
            Toast.makeText(context, "SMS not delivered",
                    Toast.LENGTH_SHORT).show();
            Log.d(Cons.TAGS+" RK","RESULT_CANCELED");
            break;
    }}
}

这是我的活动:

public class SenderPage extends Activity {
private EditText txtRecepients = null;
private EditText inputSMSText = null;
private Button btnSendSMS = null;

private final BroadcastReceiver outgoingSMSBR = new OutgoingSMSBroadcastReceiver();
private final BroadcastReceiver deliverSMSBR = new DeliverSMSBroadcastReceiver();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.senderpage);
    Log.i(Cons.TAGS, "Sender Page Start");

    this.setTitle("Send Message");

    this.txtRecepients = (EditText) findViewById(R.id.txtRecepients);
    this.inputSMSText = (EditText) findViewById(R.id.inputSMSText);
    this.btnSendSMS = (Button) findViewById(R.id.btnSendSMS);

    txtRecepients.setText("087722079905");      //087722079905 //081214571542

    btnSendSMS.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            //Toast.makeText(SenderPage.this, "Clicked send", Toast.LENGTH_SHORT).show();
            String txtRecepients = SenderPage.this.txtRecepients.getText().toString();
            String inputSMSText = SenderPage.this.inputSMSText.getText().toString();

            new Sender(SenderPage.this,Cons.TAGS).execute(txtRecepients, inputSMSText);
        }
    });

    //txtRecepients.getText().toString();
}

@Override
protected void onResume() {
    registerReceiver(outgoingSMSBR, new IntentFilter(Cons.SMS_SENT));
    registerReceiver(deliverSMSBR, new IntentFilter(Cons.SMS_DELIVERED));
    super.onResume();
}

@Override
protected void onPause() {
    unregisterReceiver(outgoingSMSBR);
    unregisterReceiver(deliverSMSBR);
    super.onPause();
}

}

问题是,当我发送多条短信时,我收到了送货信息,我无法确定哪些短信已送达?

2 个答案:

答案 0 :(得分:1)

最后,我通过sms创建日期时间为未决意图添加了意图附加内容,然后根据extras数据发送的日期时间更新sms状态得到答案,这里是片段:

Intent sentIntent = new Intent(Cons.SMS_SENT);
sentIntent.putExtra("createdTime", createdTime);

Intent deliveryIntent = new Intent(Cons.SMS_SENT);
deliveryIntent.putExtra("createdTime", createdTime);

然后我可以确定按创建时间发送/发送的短信

 public void onReceive(Context context, Intent intent) {
        String smsDateTimeAsID = intent.getStringExtra("createdTime");
 }

答案 1 :(得分:0)

Cons.SMS_SENT和Cons.SMS_DELIVERED值

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";