title: How can I keep track of sent and received messages in an android messaging app?
body: <p>I'm new to Android development (but not to Java) and I'm writing my own Android messaging app that hopefully should take the place of the default SMS app. My question is - how does the default SMS app keep track of sent and received messages and how might I accomplish the same task? Specifically, I can't figure out how to find, store, and display a conversation history between the device user and a member of their contact list. I don't have any preliminary code yet, because frankly, I have no idea where to begin.</p>
  
  <p>EDIT: Trying to set up a BroadcastReceiver as a first step (gotta start somewhere) but I'm struggling getting my app to fire when the notification comes through the device  (I'm using emulators).</p>
  
  <p>Here is my BroadcastReceiver Class (based on example from below)</p>
  
  <pre><code>import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.provider.Telephony;
  import android.telephony.SmsManager;
  import android.telephony.SmsMessage;
  import android.util.Log;
  
  
  public class smsBroadcastReceiver extends BroadcastReceiver {
      private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
      private static final String TAG = "smsBroadcastReceiver";
      private static final String SMS_SENT = "android.provider.Telephony.SMS_SENT";
      final SmsManager mySMSManager = SmsManager.getDefault();
      String phoneNumber, message;
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          if (intent.getAction().equals(SMS_RECEIVED)) handleIncMessage(intent.getExtras(), context);
          else if (intent.getAction().equals(SMS_SENT)) sendSMS(intent.getExtras(), context);
  
      }
  
      void sendSMS(Bundle bundle, Context context){
          phoneNumber = bundle.getString(Intent.EXTRA_PHONE_NUMBER);
          Log.e("info", "Outgoing Number: " + phoneNumber);
          context.sendBroadcast(new Intent("onNewMsgSend"));
      }
  
  
      void handleIncMessage(Bundle bundle, Context context) {
          if (bundle != null) {
              Object[] pdus = (Object[]) bundle.get("pdus");
              //database stuff...
              final SmsMessage[] messages = new SmsMessage[pdus.length];
              for (int i = 0; i &lt; pdus.length; i++) {
                  String sendingNum = messages[i].getDisplayOriginatingAddress();
                  messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                  String message = messages[i].getDisplayMessageBody();
  
                  Intent msgIntent = new Intent(context, conversationView.class);
                  msgIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                          | Intent.FLAG_ACTIVITY_CLEAR_TOP
                          | Intent.FLAG_ACTIVITY_NEW_TASK);
  
                  context.startActivity(msgIntent);
  
                 // Log.i(TAG, "SENDER: " + sendingNum +"; Message: " + message);
                  System.out.println("SENDER: " + sendingNum +"; Message: " + message);
              }
              context.sendBroadcast(new Intent("onNewMsg"));
          }
      }
  }
  </code></pre>
  
  <p>My best guess is that I'm doing something wrong in my Activities, but I'm not sure what. Do I need to send my intent to my main (launching) activity and then delegate the intent from there, or can I send it to an activity that isn't the launcher (which is what I'm trying to do now)?</p>
  
  <p>EDIT: BroadcastReceiver problem solved.</p>