问题描述
我正在尝试从内容提供商处读取短信。我有以下代码
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");
除非各种格式的地址出现,否则一切正常。一种类型的地址可以用各种方式表示,如+9198765443210,+ 91 987 65443210,+ 91(987)65443210,098765443210等......这些类型的各种地址格式驻留在SMS内容提供商中还有。
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.
方法1 :
最初我将所有地址转换为格式,其中特殊字符被替换by%like
+9198765443210 - >%98765443210%
+91 987 65443210 - >%987%65443210%
然后使用
String whereClause =address LIKE?;
但失败因为我们正在解雇的案例可能会出现地址LIKE%98765443210%
但是短信内容提供商的地址是+91 987 65443210.
在android中有类似normalized_address的东西,我们可以用于从SMS内容提供商处获取数据?
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 usingString 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?
推荐答案
附加到@MikeM。评论,下面一段代码帮助我获得了使用我在SMS Content Provider中进行查询的threadId
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();
这篇关于如何从Android中的特定联系人读取收件箱短信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!