如何使用"content:// sms/inbox"获取SMS发件人联系人(人)保存的名称

IPL*_*L10 10 sms android

在我的应用程序中,我想使用下面的代码检索SMS发件人保存的名称,但它始终返回null.请告诉我什么是获取发件人姓名的便捷方式.

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)+"\n"
                         +"msg : "+c.getString(11)+"\n"
                         +"ID : "+c.getString(0)+"\n"
                         +"Person : "+c.getString(3));
                    }
                }
            } catch (Exception e) {

            Log.d("mmmmmmmmm"," "+ e.getStackTrace());
            }
Run Code Online (Sandbox Code Playgroud)

我在清单中使用以下许可

 <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"/>
Run Code Online (Sandbox Code Playgroud)

请建议我如何获得.提前致谢.

kal*_*pvs 26

通过这种方式,您可以从收件箱中获取已保存的联系人姓名..调用方法 getAllSms()以获取详细信息..

 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"))
                            + "\n"
                            + "msg : "
                            + c.getColumnIndexOrThrow("body")
                            + "\n"
                            + "ID : "
                            + c.getString(c.getColumnIndexOrThrow("_id"))
                            + "\n"
                            + "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;
}
Run Code Online (Sandbox Code Playgroud)