Danny Eersel
Danny Eersel

Reputation: 1

Reading last sms from a particular sender

Im building a app that is reading last sms from spec number (vb# 8888). Code is working great. I only have one prob. When i get a new sms from other number (vb# 7777)my code stops reading sms from (v#8888). if i delete the new sms from (7777) than my code strats working again. I'm using this to update a string in my app. Can someone help me

This is my code

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
 Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri, null, null, null, null);

String[] columns = new String[]{"address", "body"};

if(cursor1.moveToFirst()) {

     String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));

if address.equalsIgnoreCase("+597*******")) {

body = cursor1.getString(cursor1.getColumnIndex(columns[3]));

Koers = (TextView) findViewById(R.id.Koersdiedag);

Koers.setText(body);//body

Upvotes: 0

Views: 1338

Answers (1)

Mike M.
Mike M.

Reputation: 39181

Your code is just reading the most recent message in the inbox, whomever it's from. If you want the most recent message from that particular number, you can adjust your query to match the number, and limit the results to one record.

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
String[] projection = {"address", "body"};
String phoneNumber = "+597*******";

Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
                                            projection,
                                            "address = ?",
                                            new String[] {phoneNumber},
                                            "date DESC LIMIT 1");

if (cursor1 != null && cursor1.moveToFirst()) {
    body = cursor1.getString(cursor1.getColumnIndex("body"));
    Koers = (TextView) findViewById(R.id.Koersdiedag);
    Koers.setText(body);
}

Upvotes: 2

Related Questions