1

I have a question about an SMS app i am creating (learning purposes).

On sending an SMS i would like to have a toast message appear once it has been deleivered (or not)

Problem is once i have hit send i would like to close the activity but still have the toast message (in this case) appear giving me the info.

As i am new to this i am not sure how to go about this. All i have done is wait for the information to come through, set up the message then close the activity.

private void sendSMS() {
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), PendingIntent.FLAG_NO_CREATE);
    deliverActivity = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                    Toast.LENGTH_LONG).show();
                    finishActivity();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                    Toast.LENGTH_LONG).show();
                    finishActivity();
                    break;           

            }
        }
    };

    registerReceiver(deliverActivity, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(toNo, null, toSend.getText().toString(), deliveredPI, null);

    ContentValues values = new ContentValues();
    values.put("address", toNo);
    values.put("body", toSend.getText().toString());
    this.getContentResolver().insert(Uri.parse("content://sms/sent"), values);

}

private void finishActivity() {
    unregisterReceiver(deliverActivity);
    Intent intent = new Intent();
    setResult(123, intent);
    finish();
}

Im sure this is possible somehow, even to pass it possibly to another activity.

It probably sounds a bit pedantic but it allows me to learn new ways of doing things... so if anyone can point me in the right direction that'd be great.

Thanks!

1 Answers1

0

There are ways to show the toast after an activity is closed, for example see this question: Android: Show toast after finishing application / activity

See this as well.

Community
  • 1
  • 1
SpeedBirdNine
  • 4,610
  • 11
  • 49
  • 67
  • Thanks for the link, will look now –  Mar 01 '12 at 21:48
  • had a look, i can get the toast to appear (as in calling it) then i finish the activity... from what i can tell this is the same as starting a thread for the toast message and then getting the thread to sleep after the amount of milliseconds the toast message is up for... no? –  Mar 02 '12 at 05:20