我是Android开发的新手。我正在做一个简单的应用程序来读取手机的文本消息。我知道围绕内容提供商content://sms有很多问题,但是...对我来说,编写一个自定义代码太难了,所以我暂时还是用它吧。
问题:当我查询content://sms/inbox时,我得到了所有的消息,所以当我试图在ListActivity中列出它们时,我得到的每条消息都有相同的联系人。基本上这是正确的,我理解它,但我需要一些其他的东西,一些“更专业”的东西。首先是因为: 1-这么大的查询在手机上需要很长时间2-显然它不会让任何感觉都以这种方式列出所有的消息。
最简单的方法就是使用DISTINCT或者...如果没有,但很糟糕,因为它再次查询所有的消息,一个GROUP BY。问题是: GROUP BY无法识别,因为正确地说,内容提供者背后的内容可能与DB不同。显然DISTINCT是被接受的,但如果使用它,它不会有任何区别。我已经抓狂了3天,在得到消息的查询后,我没有自己过滤消息就找到了解决方案。
这是我尝试执行查询的一小段简单代码: List smsList = new ArrayList();
Uri uri = Uri.parse("content://sms/inbox");
String order = new String("date DESC");
String[] projection = new String[]{"DISTINCT thread_id, address, body, _id"};
String selection = new String("GROUP BY address"); //this doesn't work
Cursor c = getContentResolver().query(uri, projection, null, null, order);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for( i=0; i < c.getCount(); i++) {
SMSData sms = new SMSData();
sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
sms.setId(c.getString(c.getColumnIndexOrThrow("_id")).toString());
sms.setPersonName(getContactNameFromNumber(sms.getNumber()));
smsList.add(sms);
//Log.v(TAG,"il nome è: "+smsList.get(i).getPersonName()+" e il numero è: "+smsList.get(i).getNumber());
c.moveToNext();
}
}
c.close();
我希望有人能帮助我!提前谢谢。
发布于 2016-11-30 23:55:38
你必须使用GROUP BY thread_id
,在我的github仓库SmsMessenger中查看更多详细信息,你可以通过以下查询获得它:
cursor = MyApplication.getContext().getContentResolver().query(Uri.parse("content://sms")
, null
, "address IS NOT NULL) GROUP BY (thread_id"
, null
, null);
https://stackoverflow.com/questions/16521107
复制相似问题