Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: sending SMS through intent with body and returning back.

I am trying to send a SMS through an intent, I want to add a body to the message. After user press send I want to return to the app. I've added extra as sms_body and exit_on_sent. But when I use them both the SMS appears without the body. If i don't use the exit_on_sent extra everything works fine.

    Intent sendIntent = new Intent(Intent.ACTION_VIEW);         
    sendIntent.setData(Uri.parse("smsto:" + phoneNumber));
    sendIntent.putExtra("sms_body", "some text");
    sendIntent.putExtra("exit_on_sent", true);
    context.startActivity(sendIntent);
like image 491
RCB Avatar asked Dec 26 '13 13:12

RCB


1 Answers

You could try using

startActivityForResult(sendIntent, SOME_REQUEST_CODE) 

but in my experience it doesn't works most of the time. I would recommend instead using SmsManager.

SmsManager smsMgr = SmsManager.getDefault();
if(smsMgr != null){
  PendingIntent sentIntent = PendingIntent.getBroadcast(
      getActivity().getApplicationContext(), 0,
      new Intent(MY_ACTION_INTENT_SENT), 0);
  smsMgr.sendTextMessage(phone, null, message, sentIntent, null);
}

Depending on your application you can do the rest of processing when MY_ACTION_INTENT is sent (indicating the message has actually been sent) or right after sendTextMessage(...) returns.

From API Level 19 there are some interesting features you may found useful http://developer.android.com/reference/android/provider/Telephony.html

Hope it helps.

like image 100
Junior Buckeridge Avatar answered Oct 11 '22 07:10

Junior Buckeridge