我在web android开发者中发现了这个功能
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)
现在,我想从content:// sms / sent获取短信,使用sms的thread_id,我将只获得最新的短信。
我将通过例子解释:
id | thread_id | phone | body
10 47
9 46
8 47
7 45
6 47
5 43
4 45
3 42
2 41
1 47
我希望收到的结果是:
id | thread_id | phone | body
10 47
9 46
7 45
5 43
3 42
2 41
我如何处理上述查询? 感谢。
答案 0 :(得分:0)
您必须使用GROUP BY thread_id
,查看我的github存储库SmsMessenger中的更多详细信息,您可以通过以下查询获取它:
Cursor cursor = MyApplication.getContext().getContentResolver().query(Uri.parse("content://sms")
, null
, "address IS NOT NULL) GROUP BY (thread_id"
, null
, null);
if (cursor != null && cursor.moveToFirst()) { // must check the result to prevent exception
do {
String messageText = cursor.getString(cursor.getColumnIndexOrThrow("body"));
System.out.println("messageText : "+messageText );
} while (cursor.moveToNext());