1

I am having a problem with some code and need some help. The code I have here sends a text... But the issue I have is that the code for receiving the text ALWAYS returns RESULT_OK... I can turn a phone off / try a number which I know is not real and it will always return RESULT_OK... Can anyone help me in the right direction, is there something obvious wrong? Have I used the wrong code? Any help would be great! Thanks

In my onCreate:

sendBroadcastReceiver = new BroadcastReceiver()
          {

            public void onReceive(Context arg0, Intent arg1)
            {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "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;
                }
            }
        };

        deliveryBroadcastReceiver = new BroadcastReceiver()
        {
            public void onReceive(Context arg0, Intent arg1)
            {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };

        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));

How I send SMS:

private void sendSMS(String phoneNumber, String message) {
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }

Manifest permissions:

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MyApplication" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.SEND_SMS" /> 

This is app specific! Everything else works just fine and the delivery report is unsuccessful! Need some major help. Any ideas? This does the same with both SendTextMessage and SendMultiPart message... Is there anything I am missing from my manifest?

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • Oops, that was you. Sorry. That's really odd. Your code looks correct. – Mike M. Aug 08 '14 at 14:55
  • Please add SMS to the title of your post. – greenapps Aug 08 '14 at 14:57
  • I dunno. I just checked, and that's pretty much exactly the code I use. (Btw, you can use the Context passed into `onReceive()` for your Toasts.) – Mike M. Aug 08 '14 at 15:03
  • You might try setting `FLAG_UPDATE_CURRENT` on the PendingIntent, but I'm not sure if that should have any effect on the broadcast's result code. – Mike M. Aug 08 '14 at 15:12
  • Looking at the source code: "The raw pdu of the status report is in the extended data ("pdu")." So you might check that. The platform SMS app might ignore the result code, and defer to actual report. – Mike M. Aug 08 '14 at 15:30
  • Toasts are there right now purely to see whats coming up and will not be used once it's working :) Is there anyway I could look at an actual report? Similar to the way the default SMS app works? – apmartin1991 Aug 08 '14 at 15:42
  • I'm assuming you decode the pdus like you would a regular SMS message, if you've done that. [This Post](http://stackoverflow.com/questions/4117701/android-sms-broadcast-receiver) has an example of decoding the pdus in the question. I wish I could test it for you, but my provider doesn't offer delivery reports. – Mike M. Aug 08 '14 at 17:11

0 Answers0