Read All Sms from a Particular Sender

Read all SMS from a particular sender

try this way:

StringBuilder smsBuilder = new StringBuilder();
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_ALL = "content://sms/";
try {
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "address='123456789'", null, "date desc");
if (cur.moveToFirst()) {
int index_Address = cur.getColumnIndex("address");
int index_Person = cur.getColumnIndex("person");
int index_Body = cur.getColumnIndex("body");
int index_Date = cur.getColumnIndex("date");
int index_Type = cur.getColumnIndex("type");
do {
String strAddress = cur.getString(index_Address);
int intPerson = cur.getInt(index_Person);
String strbody = cur.getString(index_Body);
long longDate = cur.getLong(index_Date);
int int_Type = cur.getInt(index_Type);

smsBuilder.append("[ ");
smsBuilder.append(strAddress + ", ");
smsBuilder.append(intPerson + ", ");
smsBuilder.append(strbody + ", ");
smsBuilder.append(longDate + ", ");
smsBuilder.append(int_Type);
smsBuilder.append(" ]\n\n");
} while (cur.moveToNext());

if (!cur.isClosed()) {
cur.close();
cur = null;
}
} else {
smsBuilder.append("no result!");
} // end if
}
} catch (SQLiteException ex) {
Log.d("SQLiteException", ex.getMessage());
}

AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_SMS" />

Reading last sms from a particular sender

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);
}

Reading the last 5 SMS received from a particular number on a particular date

You're only seeing one message because your code is only handling the first record in the returned Cursor. You need to loop over the Cursor to handle the rest. For example:

if (cursor != null && cursor.moveToFirst()) {
do {
body = cursor1.getString(cursor1.getColumnIndex("body"));
totalBody = totalBody + body;
Log.d("Registration", totalBody);
} while (cursor.moveToNext());
}

Also, if you want to restrict the query to one day, you can use a Calendar to figure the starting and ending times for that day in milliseconds - as that is how dates are stored in the SMS table - and add the appropriate comparison to the where clause. For example:

private static final int DAY_MILLISECONDS = 24 * 60 * 60 * 1000;
private static final Uri inboxUri = Uri.parse("content://sms/inbox");

// Months are zero-based; i.e., JANUARY == 0
// Phone number must be exact in this example
private void listMessages(String phoneNumber, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

String[] projection = {"address", "body"};
String whereAddress = "address = ?";
String whereDate = "date BETWEEN " + cal.getTimeInMillis() +
" AND " + (cal.getTimeInMillis() + DAY_MILLISECONDS);
String where = DatabaseUtils.concatenateWhere(whereAddress, whereDate);

Cursor cursor = null;
try {
cursor = getContentResolver().query(inboxUri,
projection,
where,
new String[]{phoneNumber},
"date DESC LIMIT 5");

if (cursor != null && cursor.moveToFirst()) {
do {
Log.d("Message", cursor.getString(cursor.getColumnIndex("body")));
} while (cursor.moveToNext());
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (cursor != null) {
cursor.close();
}
}
}

How to read all SMS from inbox by more than one sender but not by all sender in android?

       int argcount = MOBILENUMBER.size(); // number of IN arguments i.e More than one Mobile number for which you want read SMS

// String[] args = new String[]{ 1, 2 };
StringBuilder inList = new StringBuilder(argcount * list.length);
for (int i = 0; i < argcount; i++)
{
if(i > 0)
{
inList.append(",");
}
inList.append("?");//This will generate Number of "? " to match our item in array list
}
Cursor cur = getContentResolver().query(uri, projection, "address IN (" + inList.toString() + ")" , list, "date DESC ");

Read ALL SMS in inbox or Send it to Server in React NATIVE

True- We cannot read sms in IOS devices.

For Android, I found a library that helps to achieve this.

import SmsAndroid from 'react-native-get-sms-android'; 

var filter = {
box: 'inbox',
};

const Readsms_list = async () => {
SmsAndroid.list(
JSON.stringify(filter),
(fail) => {
console.log('Failed with this error: ' + fail);
},
(count, smsList) => {
console.log('Count: ', count);
console.log('List: ', smsList);
var arr = JSON.parse(smsList);

arr.forEach(function (object) {
// 'Object: ' +
console.log(object);
// console.log('-->' + object.date);
// console.log('-->' + object.body);
});
},

);
};

This helps in reading all the SMS from the device.

Displaying all SMS from one specific sender in my android app

apply selection arguments to filter the contents based on address field.

Actual code becomes:

String[] selectionArgs = { "12672631" }; //add the phone number here
Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);


Related Topics



Leave a reply



Submit