Reputation: 461
I am trying to read sms from content provider. I had following code
Uri uri = Uri.parse(SMS_URI_INBOX);
String whereClause = "address=?";
String[] whereArgs = {address};
String[] projection = new String[] { "*" };
Cursor cur = getContentResolver().query(uri, projection, whereClause,whereArgs, "date desc");
Everything was working fine unless address of various format came into picture. One type of addresses can be represented in various ways like "+9198765443210", "+91 987 65443210" , "+91 (987) 65443210", "098765443210" etc... These type of varied address formats reside in SMS Content provider as well.
Approach 1:
Initially I was converting all the address to format in which special characters are replaced by % like
+9198765443210 --> %98765443210%
+91 987 65443210 --> %987%65443210%
and then using
String whereClause = "address LIKE ?";
but failed because a case may come in which we are firing query address LIKE %98765443210%
but address in SMS content provider is +91 987 65443210.
Is there something like normalized_address in android which we can use to get data from SMS Content provider?
Upvotes: 4
Views: 686
Reputation: 461
Appending to @MikeM. comment, below piece of code helped me to get threadId using which I am making query in SMS Content Provider
//Getting thread Id
ContentResolver mContentResolver = context.getContentResolver();
Uri uriSmsURI1 = Uri.withAppendedPath(Telephony.MmsSms.CONTENT_FILTER_BYPHONE_URI, address);
String[] projection1 = {this.threadId};
Cursor c1 = dbService.query(mContentResolver, uriSmsURI1, projection1, null, null, null);
if(c1.getCount()==0) {
log.error(methodName, "Got count: "+c1.getCount()+" While looking for ThreadID");
return null;
}
String threadId = null;
while(c1.moveToNext()){
threadId = c1.getString(c1.getColumnIndexOrThrow(this.threadId));
}
c1.close();
Upvotes: 2