如何知道哪些短信发送/从Android的BroadcastReceiver的交付? [英] How to Know which Sms is sent/delivered from Android BroadcastReceiver?

查看:175
本文介绍了如何知道哪些短信发送/从Android的BroadcastReceiver的交付?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我做了简单的应用程序发送多个短信在Android上,我找不到知道哪些短信发送/交付的,因为交付通知并没有告诉,这就是它。

Hi Guys I have made simple app to send multiple SMS on android, and I cannot find known which sms is Sent/Delivered, because the delivery notification does not tell for which it is.

这是我的code:

    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);

然后我用广播接收器可送货状态

Then I use broadcast receiver to get delivery status

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;
    }}
}

这里是我的活动:

And Here Is my activity :

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();
}

}

和问题是,当我发送一个以上的短信,然后我得到了传递信息,我不能确保其短信发送?

And the Problem is when I sent more than One SMS then I get the delivery information I cannot make sure which SMS is delivered ?

推荐答案

最后,我得到了答案被添加的意图额外未决的意图与SMS创建的日期时间,然后根据通过额外的数据发送日期时间更新SMS状态,这里是片段:

Finally I got the answer by added intent extras to pending intent with sms create datetime, then update sms status based on datetime that sent by extras data, here is the snippet :

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

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

然后我可以找出哪些短信发送/交付时间创建

then I can identify which sms is sent/delivery by created time

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

这篇关于如何知道哪些短信发送/从Android的BroadcastReceiver的交付?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆