android sms联系人姓名

时间:2014-06-01 05:04:27

标签: android cursor sms contact

我一直在寻找几天为什么这不起作用。我正在尝试获取文本消息的发件人编号和名称,但它始终返回为零,或者无论是谁发送给我,它始终都是相同的确切数字。这个名字总是空白的。

Cursor cur = mContext.getContentResolver().query(uriReceive, null, null, null,null);

while (cur.moveToNext()) 
{
                   int index_Address = cur.getColumnIndex("address");
                   int index_Person = cur.getColumnIndex("person");

                   strAddress = cur.getString(index_Address);  
                   intPerson = cur.getString(index_Person); 
}

1 个答案:

答案 0 :(得分:1)

如果您正在阅读现有的短信(不仅仅是新来的短信)

    String columns[] = new String[] { "_id", "date", "address", "subject", "body" };

    Cursor cursor = ctx.getContentResolver().query(Uri.parse("content://sms/inbox"), columns, whereClause, bindVars, "date desc");
    //Only want newest, so only handle first item in cursor
    if(cursor.moveToFirst()){
                do{
        int id = cursor.getInt(0);
        long date = cursor.getLong(1);
        String address = cursor.getString(2);
        String subject = cursor.getString(3);
        String body = cursor.getString(4);
                    //Handle message here
            }while(cursor.moveToNext())
    }
    cursor.close();

然后,要将地址转换为姓名,您需要与联系人数据库交叉引用地址。