0

My TextMessage activity is passed a name and phone number, and then proceeds to allow the user to type a text message, and send the message on.

My activity does what it is supposed to, however, after sending the message it calls my TextMessage activity again but it doesn't retain the name and phone number.

Is there a way I can do that? I know I can't use "putExtra" for a pendingIntent, so I'm at a loss as to how to do this.

Also, my cancel button crashes in this circumstance because the name and phone number that I received from the previous Intent are now null.

My code:

public class TextMessage extends Activity {

    private Button cancel;
    private EditText message;
    private TextView nameInfo;
    private Button send;

    private String searchName;
    private String searchPhone;

    private SmsManager smsManager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);

        cancel = (Button) findViewById(R.id.cancelmessage);
        send = (Button) findViewById(R.id.sendmessage);
        message = (EditText) findViewById(R.id.messagebodytext);
        nameInfo = (TextView) findViewById(R.id.messageuser);

        searchName = getIntent().getStringExtra("name");
        searchPhone = getIntent().getStringExtra("phonenumber");    

        nameInfo.setText(searchName + ": " + searchPhone);

        smsManager = SmsManager.getDefault();
        //final PendingIntent sentIntent = PendingIntent.getActivity(this, 0, new Intent(this, TextMessage.class), 0);
        Intent intent = new Intent(this, TextMessage.class);
        intent.putExtra("name", searchName);
        intent.putExtra("phonenumber", searchPhone);
        final PendingIntent sentIntent = PendingIntent.getActivity(this, 0, intent, 0);

        send.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                String messageBody = message.getText().toString();
                if (messageBody.length() > 0) {
                    smsManager.sendTextMessage(searchPhone, null, messageBody, sentIntent, null);
                    finish();
                }
            }
        });

        cancel.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(TextMessage.this, SMS.class);
                intent.putExtra("name", searchName);
                startActivity(intent);
                finish();
            }
        });
    }
}
Jay Lefler
  • 315
  • 3
  • 5
  • 13

1 Answers1

1

Instead of using this in the constructor of the pending intent. You should created it outside and added your extras to that intent. I don't think it matters for this case as much but if you had another pending intent with the same information you would need the update current to override the extras.

 intent = new Intent(this, TextMessage.class);
 intent.putExtra("","");
 final PendingIntent sentIntent = PendingIntent.getActivity(this, 0, intent, 0);
caguilar187
  • 753
  • 3
  • 10
  • That isn't working for me, unfortunately. It is still returning me to the screen with null values for the name and phone number, even when I used putExtra into the intent for the PendingIntent. – Jay Lefler Nov 13 '11 at 02:04
  • give this post a try. http://stackoverflow.com/questions/4340431/how-can-i-correctly-pass-unique-extras-to-a-pending-intent – caguilar187 Nov 13 '11 at 02:25
  • i wish that made sense to me, i dont know how to implement those things – Jay Lefler Nov 13 '11 at 02:35
  • Actually I set a PendingIntent.FLAG_UPDATE_CURRENT and everthing started working. Thanks for the help. – Jay Lefler Nov 13 '11 at 02:40