【问题标题】:How to read inbox sms from a specific contact in android?如何从android中的特定联系人读取收件箱短信?
【发布时间】:2018-04-14 20:14:23
【问题描述】:

我正在尝试从内容提供商处读取短信。我有以下代码

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 内容提供程序中也是。

方法 1
最初,我将所有地址转换为特殊字符替换为 % 的格式,例如
+9198765443210 --> %98765443210%
+91 987 65443210 --> %987%65443210%
然后使用 String whereClause = "address LIKE ?";
但失败了,因为我们可能会触发查询 address LIKE %98765443210% 但 SMS 内容提供商中的地址是 +91 987 65443210。

android 中是否有类似 normalized_address 的东西,我们可以使用它从 SMS 内容提供程序获取数据?

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    附加到@MikeM。评论,下面的代码帮助我获得了我在 SMS Content Provider 中查询时使用的 threadId

    //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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多