我已经搜索了好几天了,为什么这不起作用。我试图获取短信的发件人号码和姓名,但它总是返回为零,或者无论谁给我发短信,它总是返回相同的数字。并且名称始终为空。
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);
}
最佳答案
如果您正在阅读现有的短信(不仅仅是收到的新短信)
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();
然后,要将地址转换为名称,您需要将地址与联系人数据库进行交叉引用。
关于Android 短信联系人姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23976926/