Reputation: 19
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
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