将数据传递到Broadcast Receiver以进行SMS传送报告

时间:2014-02-14 13:26:40

标签: android android-intent sms broadcastreceiver

来自我使用putExtra(String,String)将数据传递给广播接收器的活动,但问题是,在第一条SMS发出几秒钟后,当第二条短信发送到不同的名称和号码时,名称和电话号码没有改变。

当我等待很长时间并发送短信时,名称和号码被更改为正确的名称和号码。为什么呢?

如何解决这个问题?

                 SmsManager SM1=SmsManager.getDefault();
                 ArrayList<String> parts = SM1.divideMessage("Hello");
                 ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
                 ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

                 size=parts.size();
                 for (int i=1;i<=size;i++) {

                  Intent in=new Intent("package.DeliveryReport.SMS_DELIVERED");
                 in.putExtra("package.DeliveryReport.name", name);
                 in.putExtra("package.DeliveryReport.phoneNumber", phoneNumber);
                 deliveryIntents.add(PendingIntent.getBroadcast(this, 0,
                         in, 0));

                 }

                SM1.sendMultipartTextMessage(phoneNumber,null,parts,sentIntents,deliveryIntents);

广播接收器

public class DeliveryReport extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {


    if(intent.getAction().equals("package.DeliveryReport.SMS_DELIVERED"))
    {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
          switch (getResultCode())
          {
            case Activity.RESULT_OK:
                vibrator.vibrate(500);
                Toast.makeText(context, "SMS DELIVERED to "+intent.getStringExtra("package.DeliveryReport.name")+
                        " phone "+intent.getStringExtra("package.DeliveryReport.phoneNumber"),Toast.LENGTH_SHORT).show();

              break;
            case Activity.RESULT_CANCELED:
                vibrator.vibrate(500);
                Toast.makeText(context, "SMS did not DELIVERED to "+intent.getStringExtra("package.DeliveryReport.name")+
                        " phone "+intent.getStringExtra("package.DeliveryReport.phoneNumber"),Toast.LENGTH_SHORT).show();
              break;
            }
          }


}

1 个答案:

答案 0 :(得分:0)

我需要的只是添加PendingIntent.FLAG_ONE_SHOT

Intent in=new Intent("package.DeliveryReport.SMS_DELIVERED");
             in.putExtra("package.DeliveryReport.name", name);
             in.putExtra("package.DeliveryReport.phoneNumber", phoneNumber);
             deliveryIntents.add(PendingIntent.getBroadcast(this, 0,
                     in, PendingIntent.FLAG_ONE_SHOT));