1

I want to read a specific SMS in my Inbox. I found on the Internet how to read all the sms in the inbox. That is what I did. Please help me to read just one SMS from a specific number. Thanks

package com.example.liresms;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class ReadSMS extends MainActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView view = new TextView(this);
      Uri uriSMSURI = Uri.parse("content://sms/inbox");
      Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
      String sms = "";
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";         
      }
      view.setText(sms);
      setContentView(view);
  }
}
Expedito
  • 7,771
  • 5
  • 30
  • 43
user2207848
  • 47
  • 3
  • 9

1 Answers1

0

You're pretty close to already doing it. I suggest you take a look at the arguments for the ContentResolver.query method and pay specific attention to the selection parameter. What you're looking to do is only select messages where a particular column is equal to the number you're looking for...

Something like

Cursor cur = getContentResolver().query(uriSMSURI, null, "from=6159995555", null,null);

I don't know the specific column name off the top of my head, but that should get you started in the right direction...

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109