android - 计算单个线程的已发送短信

标签 android sms

我正在查找特定线程(例如,id 为 15)中已发送短信的计数。我找到了这个 How do I get the count of SMS messages per contact into a textview? <-- 但它并没有解决我的问题,因为它计算了发送和接收的短信。是否可以只计算已发送的消息?我想我可以查询“content://sms/sent”并遍历每条 SMS,但我想知道是否有更有效的方法。

谢谢

最佳答案

您可以使用线程 ID 查询 Sms.Conversations,并选择将 TYPE 列限制为 MESSAGE_TYPE_SENT。由于您只需要计数,我们可以执行 SELECT COUNT() 查询,因此不会浪费资源来构建具有未使用值的 Cursor。例如:

private int getThreadSentCount(String threadId) {
    final Uri uri = Sms.Conversations.CONTENT_URI
                    .buildUpon()
                    .appendEncodedPath(threadId)
                    .build();
    final String[] projection = {"COUNT(1)"};
    final String selection = Sms.TYPE + "=" + Sms.MESSAGE_TYPE_SENT;

    int count = -1;
    Cursor cursor = null;
    try {
        cursor = getContentResolver().query(uri,
                                            projection,
                                            selection,
                                            null,
                                            null);
        if (cursor != null && cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return count;
}

上面使用的Sms类在android.provider.Telephony类中。

import android.provider.Telephony.Sms;

作为引用,Sms.Conversations.CONTENT_URI 等同于 Uri.parse("content://sms/conversations")Sms.TYPE"type"Sms.MESSAGE_TYPE_SENT2

关于android - 计算单个线程的已发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43122820/

相关文章:

android - XML 选择器背景不适用于 2.3

android - 极其基本的 : Switching between Activities (Android)

android - adb:无法访问或未找到

android - 检查设备是否可以接收短信

android - 如何从 android 中的微调器中获取值(value)?

java - Android-以编程方式发送彩信而不是默认应用程序

sms - 来自短信的位置数据

从电子邮件中发送的链接打开 Android 应用程序

android - PlayStore新视差效果怎么做

php - 用 PHP 发送和接收短信