Ahsan
Ahsan

Reputation: 19

How to send message using back button

In Android, I am creating voice to text app in which I want to send message using back key of cell phone. I did this in onBackPressed() method. When I press back button, it shows "Your message sent successfully", but actually does not send. Here is my code. Please help me. How I can do it?

public void onBackPressed() {
try {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
    Toast.makeText(getApplicationContext(), "Your Message Sent Successfully",
            Toast.LENGTH_LONG).show();
} catch (Exception e) {
    Toast.makeText(getApplicationContext(),
            "Sending fail, Please try again!",
            Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

super.onBackPressed();

}

I toast phoneNo and sms it show but don't know what is the fault in this code?

Upvotes: 0

Views: 229

Answers (1)

Vovchik
Vovchik

Reputation: 244

sorry, can't comment your post, so I write this like an answer.

are you tried to use listener: check this link?

One of params. is:

sentIntent - if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will be Activity.RESULT_OK for success.

Check this answer HTH;

Upvotes: 2

Related Questions