2 qq 34956435 qq_34956435 于 2016.05.11 10:39 提问

求助一个关于安卓短信sms的问题

int nameColumn = cur.getColumnIndex("person");
用这个获取短信person字段不是应该是获得短信的发件人id么,我怎么全部都是2啊
搞得我好2
代码:

public String getSmsInPhone()

{

final String SMS_URI_ALL = "content://sms/";

final String SMS_URI_INBOX = "content://sms/inbox";

final String SMS_URI_SEND = "content://sms/sent";

final String SMS_URI_DRAFT = "content://sms/draft";

StringBuilder smsBuilder = new StringBuilder();   

try{   
    ContentResolver cr = getContentResolver();   
    String[] projection = new String[]{"_id", "address", "person",    
            "body", "date", "type"};   
    Uri uri = Uri.parse(SMS_URI_ALL);   
    Cursor cur = cr.query(uri, projection, null, null, "date desc");   

    if (cur.moveToFirst()) {   
        String name;    
        String phoneNumber;          
        String smsbody;   
        String date;   
        String type;   

        int nameColumn = cur.getColumnIndex("person");   
        int phoneNumberColumn = cur.getColumnIndex("address");   
        int smsbodyColumn = cur.getColumnIndex("body");   
        int dateColumn = cur.getColumnIndex("date");   
        int typeColumn = cur.getColumnIndex("type");   

        do{   
            name = cur.getString(nameColumn);                
            phoneNumber = cur.getString(phoneNumberColumn);   
            smsbody = cur.getString(smsbodyColumn);   

            SimpleDateFormat dateFormat = new SimpleDateFormat(   
                    "yyyy-MM-dd hh:mm:ss");   
            Date d = new Date(Long.parseLong(cur.getString(dateColumn)));   
            date = dateFormat.format(d);   

            int typeId = cur.getInt(typeColumn);   
            if(typeId == 1){   
                type = "接收";   
            } else if(typeId == 2){   
                type = "发送";   
            } else {   
                type = "";   
            }   

            smsBuilder.append("[");   
            smsBuilder.append(name+",");   
            smsBuilder.append(phoneNumber+",");   
            smsBuilder.append(smsbody+",");   
            smsBuilder.append(date+",");   
            smsBuilder.append(type);   
            smsBuilder.append("] ");   

            if(smsbody == null) smsbody = "";     
        }while(cur.moveToNext());   
    } else {   
        smsBuilder.append("no result!");   
    }   

    smsBuilder.append("getSmsInPhone has executed!");   
} catch(SQLiteException ex) {   
    Log.d("SQLiteException in getSmsInPhone", ex.getMessage());   
}   
return smsBuilder.toString();   

}

2个回答

lbcab
lbcab   2016.05.11 10:46

cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
获取联系人名字

lbcab
lbcab 回复俯览风景: http://www.2cto.com/kf/201408/323456.html
一年多之前 回复
qq_34956435
qq_34956435 回复lbcab: 大神,短信收件人这个你还会么
一年多之前 回复
lbcab
lbcab 回复俯览风景: ..............
一年多之前 回复
qq_34956435
qq_34956435 回复lbcab: 哦,我写错了,是要获取发件人的姓名
一年多之前 回复
lbcab
lbcab ContactsContract.CommonDataKinds.Phone是一个封装类它封装好了如何去解析uri字符串, 只需要传入DISPLAY_NAME, 就能返回联系认得姓名
一年多之前 回复
qq_34956435
qq_34956435 最里面那个括号里的看不太懂,能解释下么,谢谢
一年多之前 回复
CSDNXIAOD
CSDNXIAOD   2016.05.17 07:20

短信SMS
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!