如何在BroadcastReceiver中获取传送报告

时间:2013-05-22 18:20:17

标签: android broadcastreceiver android-pendingintent

我正在创建一个接收短信并发送特定号码的应用。我想在BroadcastReceiver类中获取状态报告(用于发送和传递)。

以下是BroadcastReceiver的代码:

public class SmsReceiver extends BroadcastReceiver {

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

        Bundle extras = intent.getExtras();
        String messages = "";
        String number = "6999999999";

        if (extras != null) 
        {
            // Get array data from SMS
            Object[] smsExtra = (Object[]) extras.get("pdus"); // "pdus" is the key

            for (int i=0; i<smsExtra.length; ++i) 
            {       
                // ------------------- SMS ------------------------------
                SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);              
                byte[] sms_body = null;
                try {
                    sms_body = sms.getMessageBody().getBytes("UTF-8");
                } catch (UnsupportedEncodingException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                String sms_address = sms.getOriginatingAddress();


                sendSMS(number, "2", context);


            }// end for
        }// end if                                  

    }//end onReceive 

我找到了以下获取投放报告的代码行。将它们包含在BroadcastReceiver类中是不可能的,因为我在registerReceiver上遇到错误。 任何帮助?

//---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message,final Context context)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 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(), "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,只是从上下文中获取了registerReceiver

//---when the SMS has been sent---
       context.registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        if (flag_sent == 0){    
..
}