在android中查询来自SMS的联系人姓名

时间:2014-09-28 16:29:34

标签: android sqlite

我从" content:// sms / sent"查询联系人姓名时遇到问题。虽然有特定短信的联系人姓名,但我的查询中没有来自人员栏目的联系人姓名。如果查询短信有问题,我会查询以下代码并协助我。

private ArrayList<Model> getSMSDetails(){
    ArrayList<Model> models = new ArrayList<Model>();
    Cursor managedCursor = getActivity().getContentResolver().query(Uri.parse("content://sms/sent"),
            null, null, null, Telephony.Sms.ADDRESS + " ASC");

    int number = managedCursor.getColumnIndex(Telephony.Sms.ADDRESS);
    int person = managedCursor.getColumnIndex(Telephony.Sms.PERSON);
    int boty = managedCursor.getColumnIndex(Telephony.Sms.BODY);

    int totalMessage = 0;
    models.add(new Model("Total Sent Message : "));

    while (managedCursor.moveToNext()){
        String phNumber = managedCursor.getString(number);
        String contactName = managedCursor.getString(person);
        String messageBody = managedCursor.getString(boty);

        Model model = new Model();
        if(contactName == null){
            model.setContactName(phNumber);
        }
        else{
            model.setContactName(contactName);
        }
        model.setBody(messageBody);
        model.setIcon(R.drawable.ic_sms);

        models.add(model);
        totalMessage++;
    }

    Model model = models.get(0);
    model.setTitle("Total Outgoing SMS - " + totalMessage);

    return  models;
}

1 个答案:

答案 0 :(得分:1)

我通过短信查询中的电话号码查询联系人数据库来解决问题。

while (managedCursor.moveToNext()){
        String phNumber = managedCursor.getString(number);
        String contactName = managedCursor.getString(person);
        String messageBody = managedCursor.getString(boty);

        Model model = new Model();
        if(contactName == null || contactName == "" || contactName == "0"){
            contactName = getContactName(getActivity(),phNumber);
            if(contactName == null || contactName == ""){
                contactName = phNumber;
            }
        }
        else{

        }

        model.setContactName(contactName);
        model.setTitle(messageBody);
        model.setIcon(R.drawable.ic_sms);
        model.setCounter("");
        model.setLabel("");
        model.setCountVisible(false);
        model.setLabelVisible(false);
        model.setCallDate("");

        models.add(model);
        totalMessage++;
    }

我写了getContactName(Context context,String phoneNumber)如下。

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

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

但是,从性能视图来看这可能很糟糕,因为它在while循环中一次又一次地查询数据库。如果我找到了更好的解决方案,我会更新。

相关问题