【问题标题】:Count sent sms messages for a single thread计算单个线程的已发送短信
【发布时间】:2017-03-30 15:58:42
【问题描述】:

我正在查找特定线程(例如,ID 为 15)中已发送短信的计数。我发现了这个How do I get the count of SMS messages per contact into a textview?

谢谢

【问题讨论】:

    标签: android sms


    【解决方案1】:

    您可以使用您的线程 ID 和将 TYPE 列限制为 MESSAGE_TYPE_SENT 的选择来查询 Sms.Conversations。由于您只需要计数,我们可以执行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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多