Java 安卓2.2阅读短信收件箱消息

Java 安卓2.2阅读短信收件箱消息,java,android,sms,Java,Android,Sms,我能够阅读短信收件箱并显示所有信息。我有两个问题。第一个是person字段始终为空 这是我对content://sms/inbox“ “人员”字段始终为空。有没有办法检索此SMS消息的实际显示名称?是否存在到另一个表的联接以检索显示名称 第二,当我在手机上测试时,我注意到我的短信没有包含在查询中。为什么会这样?有可能解决这个问题吗 提前谢谢 The person field is always NULL. Is there a way to retrieve the actual display

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

这是我对content://sms/inbox“

“人员”字段始终为空。有没有办法检索此SMS消息的实际显示名称?是否存在到另一个表的联接以检索显示名称

第二,当我在手机上测试时,我注意到我的短信没有包含在查询中。为什么会这样?有可能解决这个问题吗

提前谢谢

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

在这种情况下,您得到的地址是数字格式的发件人地址,因此您需要在联系人查询中传递此号码以检索此人的姓名

请尝试以下代码:

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

还请注意,如果手机列出了多个帐户,则短信/收件箱联系人不会直接与contacts.contract.\u id列对应。请参阅以寻求帮助。

是的,我刚刚意识到这一点。我得再查询一下联系人。看到我发送的短信也是一样,可能是查询发送的内容存储而不是收件箱。我是Android新手,所以仍然习惯于这些内容商店。谢谢
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;
        }