Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Delivery Report for sent text message? [duplicate]

Tags:

android

sms

In my Application I'm sending text sms using SMS Manager. To check whether messages is sent or not I'm using Toast. Instead of Toast I want to get SMS Delivery Report.I am trying many examples but, one flow not displaying delivery report like if my balance is 0 when SMS sending in my code I want to find SMS sending failed delivery report when my balance is 0.

Code:

String phoneNo = phno.getText().toString();
String smsBody = body.getText().toString(); 
 //Selected Messages to report to 1909
{
             try
             {
                
                 SmsManager smsManager = SmsManager.getDefault();
                 smsManager.sendTextMessage(phoneNo, null, smsBody, null, null); 
                 Toast.makeText(getApplicationContext(), "SMS Sent to: "+phoneNo,Toast.LENGTH_SHORT).show();
             }                           
             catch (Exception e)
             {
                 //Error Occurred if No Messages Selected 
                 Toast.makeText(getApplicationContext(),"SMS failed, please try again later!",Toast.LENGTH_SHORT).show();
                 e.printStackTrace();
             }
             }
         
    }
like image 598
Android beginner Avatar asked Feb 14 '23 15:02

Android beginner


2 Answers

try this link,

SMS Delivery Report in Android

 class deliverReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, 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;
            }

        }
    }

don't forgot to add these permissions in manifest

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.SEND_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
<uses-permission android:name="android.permission.WRITE_SMS" /> 
like image 105
Anjali Tripathi Avatar answered Feb 16 '23 05:02

Anjali Tripathi


to send sms use this method

public void sendTextMessage (String destinationAddress, String scAddress, String text, 
PendingIntent sentIntent, PendingIntent deliveryIntent)

eg

sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

where deliveredPI is PendingIntent.

the last parameter is deliveryIntent which is fired by android on "successful delivery of message" to the recipient.

so now u need to have a BroadcastReceiver to get this msg.

BroadcastReceiver deliveryBroadcastReciever = new deliverReceiver();

class deliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, 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;
        }

    }
}
like image 24
kevz Avatar answered Feb 16 '23 06:02

kevz