Search code examples
androidsmsbroadcastreceivertoast

Processing received SMS as a TOAST in Android


I need your help as you used to do. The portion of my app is about sending and receiving SMS in android. I done quite lot research especially past posting in this forum. But i didnt get the answer i am looking for. I am able to send and receive SMS at the receiving android phone. The important thing is that i want to process the received SMS to display it as TOAST. It only goes to inbox with notification but no TOAST. Meanwhile, i am receiving my outgoing call number in my broadcastReceiver class and i am wondering about the specification for processing SMS as TOAST. Please help me!

Here is my Receiver class for SMS Processing as TOAST. It is not mine really:

import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;

    public class Receiver extends BroadcastReceiver
    {

      @Override
      public void onReceive(Context context, Intent intent)
      {
        Bundle bundle = intent.getExtras();

        String recMsgString = "";
        String fromAddress = "";

        SmsMessage recMsg = null;
        byte[] data = null;

        if (bundle != null)
        {
          //---retrieve the SMS message received---
          Object[] pdus = (Object[]) bundle.get("pdus");
          for (int i = 0; i < pdus.length; i++)
          {
            recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);

            try
            {
              data = recMsg.getUserData();
            }
            catch (Exception e)
            {

            }
            if (data != null)
            {
              for (int index = 0; index < data.length; ++index)
              {
                recMsgString += Character.toString((char) data[index]);
              }
            }

            fromAddress = recMsg.getOriginatingAddress();
          }
        }

        Intent result = new Intent(context, ReceiveActivity.class);
        result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(result);
      }
    }




    ReceiverActivity.java



           public class ReceiverActivity extends Activity
            {

              @Override
              protected void onCreate(Bundle savedInstanceState)
              {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                Intent intent = getIntent();

                String message = intent.getStringExtra(MESSAGE);
                Log.i("This is the message", message);

                if (message != null)
                {
                    Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
                }
              }
            }

Solution

  • Try like this:

        /*
        Intent result = new Intent(context, ReceiveActivity.class);
        result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
        result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(result);
        */
        Toast.makeText(context, recMsgString, Toast.LENGTH_SHORT).show();