开发者

Android 2.2 Read SMS Inbox messages

I'm able to read SMS Inbox and display all messages. I have two problems. The first one is that the person field is always NULL.

This is my query to the "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);

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?

Second, when I tested on my phone, I noticed that my SMS messages are not included in the query. Why is that so? Is it possible to fix this?

Thanks in advance.


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?

For display name you have use another query and cursor or you can join the query to fetch the name of the person but sometime you didn't get the name because that person contact not saved in your contact list or any another organization or company will send the message then you didn't get the name so at that time can query on contact table to fetch the name if found then you can display it otherwise display the address as default.

In this you got the address that was the sender address as in number format so you need to pass this number in the contact query to retrieve the name of that person


Also be advised the sms/inbox person does not correspond directly to the contacts.contract._id column if the phone has multiple accounts listed. See http://developer.android.com/reference/android/provider/ContactsContract.html for help.


Try this code:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜