Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

android - How do i get the SMS Sender Contact (Person) saved name using "content://sms/inbox"

In my App i want to retrieve the SMS sender saved Name using below code but it always return null. Please suggest me whats the convenient way to get the sender name.

Uri SMS_INBOX = Uri.parse("content://sms/inbox");
         Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
         android.util.Log.i("COLUMNS", Arrays.toString(c.getColumnNames()));

            try {
                if(c.getCount()>0)
                {
                    while (c.moveToNext()){
                     Log.d("SMSss", "Contact : "+c.getString(2)+"
"
                         +"msg : "+c.getString(11)+"
"
                         +"ID : "+c.getString(0)+"
"
                         +"Person : "+c.getString(3));
                    }
                }
            } catch (Exception e) {

            Log.d("mmmmmmmmm"," "+ e.getStackTrace());
            }

i am using following permission in menifest

 <uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

Please suggest me how to get. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By this way you can get the saved contact name from inbox.. call the method getAllSms() to get the details..

 public void getAllSms() {
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(message, null, null, null, null);
    startManagingCursor(c);
    int totalSMS = c.getCount();
    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            Log.d("SMSss",
                    "Contact number : "
                            + c.getString(c
                                    .getColumnIndexOrThrow("address"))
                            + "
"
                            + "msg : "
                            + c.getColumnIndexOrThrow("body")
                            + "
"
                            + "ID : "
                            + c.getString(c.getColumnIndexOrThrow("_id"))
                            + "
"
                            + "Person : "
                            + getContactName(
                                getApplicationContext(),
                                    c.getString(c
                                            .getColumnIndexOrThrow("address"))));

            c.moveToNext();
        }
    }
    c.close();

}

public String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri,
            new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if (cursor.moveToFirst()) {
        contactName = cursor.getString(cursor
                .getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...