Question

Is it possible that i can get Notification when Handset sends SMS and get the receiver's Number after the SMS is sent. I used BroadcasrReceiver to get notification of Incoming Message.But in same way i am not able to track outgoing SMS message by using "android.provider.Telephony.SMS_SENT"

Here is My code:

This is to get notification when sms received:

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }    
}

This i used to send sms:

public class MainActivity extends Activity {

    EditText e1,e2;
    Button b;
    String sms,phoneNo;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);

        b=(Button) findViewById(R.id.button1);

        b.setOnClickListener(new OnClickListener() {




            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub





                sms=e1.getText().toString();
                //sms=e1.getText().toString();

                phoneNo=e2.getText().toString();



                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);



                Toast.makeText(MainActivity.this, "DelivereD", Toast.LENGTH_LONG).show();




            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;



    }

}

I need to get notify after the sms delivered . Plzzz help me. Thanks in advance :)

Was it helpful?

Solution

void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) is the complete method that you want to use. in the DeliveryIntent i.e. the 5th arguement of the method rather than keeping it empty you can use this to do what you want to do. Create an intent which will for example start another activity an wrap that intent in a pendingIntent and use that in method. Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top