开发者

how to get the contact infomation by sms message

开发者 https://www.devze.com 2023-01-30 20:15 出处:网络
I\'m working on an extend SMS application. And now i can read all the SMS message from the mmssms.db. In the SMS database table, the field \'person\' indicate the \'_id\' in contact table. When \'pers

I'm working on an extend SMS application. And now i can read all the SMS message from the mmssms.db. In the SMS database table, the field 'person' indicate the '_id' in contact table. When 'person' is >= 1, that means the message is sent from people in the contact list. So i can 'managedQuery' from contact table. But the question is, in my mobile phone, the test program sometimes can NOT query the contact infomation even 'person' >= 1. So can somebody show me some correct ways to query contact infomation by 'person' filed in SMS table ? Here is some sample code which can make my question more clear:

class ContactItem {  
    public String mName;  
}  

    ContactItem getContact(Activity activity, final SMSItem sms) {  
        if(sms.mPerson == 0) return null;  
        Cursor cur = activity.managedQuery(ContactsContract.Contacts.CONTENT_URI,   
                new String[] {PhoneLookup.DISPLAY_NAME},   
                " _id=?",   
               new String[] {String.valueOf(sms.mPerson)}, null);  
       if(cur != null &&  
           cur.moveToFirst()) {  
           int idx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
           ContactItem item = new ContactItem();  
           item.mName = cur.getString(idx);  
           re开发者_Go百科turn item;  
       }  
       return null;  
   }  


Ok, as there is no one help me out, i tried to read some open source project code, and i got answer now. As i supposed at the very beginning, the best way to query the contact information from a SMS message, is to query by the NUMBER(also know as ADDRESS):

   ContactItem getContactByAddr(Context context, final SMSItem sms) {  
    Uri personUri = Uri.withAppendedPath(  
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI, sms.mAddress);  
    Cursor cur = context.getContentResolver().query(personUri,  
            new String[] { PhoneLookup.DISPLAY_NAME },  
            null, null, null );  
    if( cur.moveToFirst() ) {  
        int nameIdx = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
        ContactItem item = new ContactItem();  
        item.mName = cur.getString(nameIdx);  
       cur.close();  
       return item;  
   }  
   return null;  

}

0

精彩评论

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

关注公众号