Android 如何正确显示收到的短信发件人和日期?

Android 如何正确显示收到的短信发件人和日期?,android,sms,android-contentresolver,Android,Sms,Android Contentresolver,我有以下代码: Uri uriSMS = Uri.parse("content://sms"); Cursor cur = getContentResolver().query(uriSMS, new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"}, null, null, null); startManagingCursor(cur); String[] f

我有以下代码:

Uri uriSMS = Uri.parse("content://sms");
Cursor cur = getContentResolver().query(uriSMS, 
        new String[] {"_id", "thread_id", "address", "person", "date", "body", "type"},
        null, null, null);
startManagingCursor(cur);

String[] from = new String[]{"address", "body", "date"};
int[] to = new int[]{R.id.sms_from, R.id.sms_body, R.id.sms_date};
adapter = new SimpleCursorAdapter(this, R.layout.sms_row, cur, from, to);

setListAdapter(adapter);
目前我使用
地址
列显示短信发送者,因此显示电话号码。我想用人名代替电话号码。我希望
person
包含它,但对于大多数短信来说它是空的。 然后,
date
字段包含
long
值,我希望以
DD.MM.YYYY
格式显示该值。我想我可以重写
setViewValue()
来修复它。但还有其他(更好的)解决方案吗&


如何解决这两个问题?

我认为对于联系人中不存在的号码,
person
为空

截止日期:

String strDateTime = (String) DateFormat.format("yyyy-MM-dd kk:mm", new Date(longValue));

要解决日期问题,您可以这样做

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    formatter.format( Long.parseLong( dateString ) );  
对于发件人名称,请尝试以下操作

    Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},condition,null,null);  
    cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));

尝试此方法在sms中显示正确的日期和时间。在这种情况下,您需要传递从游标接收的日期

public String changeDate(long date) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date dt = new Date(date);
        Date now = new Date();
        dt.setTime(date);

        if (now.getYear()==dt.getYear() && now.getMonth()==dt.getMonth() && now.getDate()==dt.getDate())
            sdf.applyPattern("h:mm a");
        else if (now.getYear()==dt.getYear())
            sdf.applyPattern("d MMM" + " " + "h:mm a");
        else
            sdf.applyPattern("d MMM yyyy" + " " + "h:mm a");

        return sdf.format(dt);
    }

person
对于几乎所有SMS都是空的。我不需要一些额外的权限才能使用它吗?我不知道试试:
谢谢。你的回答没有完全回答我的问题。所以,我已经用
setViewValue()
做了我需要的,但是你的回答有帮助,所以我接受了。谢谢