【问题标题】:Android 2.2 Read SMS Inbox messagesAndroid 2.2 阅读短信收件箱消息
【发布时间】:2011-12-27 15:32:30
【问题描述】:

我能够阅读短信收件箱并显示所有消息。我有两个问题。第一个是 person 字段始终为 NULL。

这是我对“content://sms/inbox”的查询。

Cursor cursor = context.getContentResolver().query(SMS_INBOX_CONTENT_URI,
           new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                     null,
                     null,
                     SORT_ORDER);

person 字段始终为 NULL。有没有办法检索此 SMS 消息的实际显示名称?是否有连接到另一个表以检索显示名称?

其次,当我在手机上进行测试时,我注意到我的 SMS 消息没有包含在查询中。为什么呢?有可能解决这个问题吗?

提前致谢。

【问题讨论】:

    标签: java android sms


    【解决方案1】:
    The person field is always NULL. Is there a way to retrieve the actual display name for
    
    
    this SMS message? Is there a join to another table to retrieve display name?
    

    对于显示名称,您使用了另一个查询和光标,或者您可以加入查询以获取该人的姓名,但有时您没有获得姓名,因为该人的联系人未保存在您的联系人列表或任何其他组织中,或者公司将发送消息,然后您没有得到姓名,因此当时可以在联系表上查询以获取姓名,如果找到,然后您可以显示它,否则默认显示地址。

    在此您获得了作为数字格式的发件人地址的地址,因此您需要在联系人查询中传递此号码以检索该人的姓名

    【讨论】:

    • 是的,刚刚意识到这一点。我将不得不再次查询联系人。看到我发送的 SMS 消息也是如此,可能查询已发送内容存储而不是收件箱。我是 Android 新手,所以仍然习惯这些内容商店。谢谢。
    【解决方案2】:

    如果手机列出了多个帐户,还请注意,短信/收件箱人员不直接对应于 contacts.contract._id 列。请参阅http://developer.android.com/reference/android/provider/ContactsContract.html 寻求帮助。

    【讨论】:

      【解决方案3】:

      试试这个代码:

      public String findNameByAddress(Context ct,String addr)
              {
                   Uri myPerson = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
                              Uri.encode(addr));
      
                   String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
      
                      Cursor cursor = ct.getContentResolver().query(myPerson,
                              projection, null, null, null);
      
                      if (cursor.moveToFirst()) {
      
      
      
                          String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      
      
                          Log.e("","Found contact name");
      
                          cursor.close();
      
                          return name;
                      }
      
                      cursor.close();
                      Log.e("","Not Found contact name");
      
                      return addr;
              }
      

      【讨论】:

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