查询SMS消息将返回null游标

时间:2015-02-25 15:14:02

标签: android sms

我们需要同时阅读手机的入站和出站短信。我做了很多谷歌搜索,并找到了许多来源,如:

Detecting SMS incoming and outgoing How to use SMS content provider? Where are the docs? Android read SMS messages

仅举几例。

根据我的研究,我编写了以下代码:

    Uri allMessages = Uri.parse("content://sms/inbox");
    Cursor cursor = airApp.getContentResolver().query(allMessages, null, null, null, null);

    if(cursor.moveToFirst()){
        do {
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                log.debug(cursor.getColumnName(i) + "=" + cursor.getString(i) + "");
            }
            log.debug("One row finished **************************************************");
        } while (cursor.moveToNext());
    }

问题是游标总是为空。我检查了权限,它们是:

    <uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />

为什么我会得到一个空游标?

1 个答案:

答案 0 :(得分:0)

这是我收集用户消息的方式

Context mContext = c;

// The category can be only "inbox" or "sent" or"outbox"
String category = "inbox";

        Cursor c = tempContext.getContentResolver().query(Uri.parse("content://sms/" + category), null, null, null, null);

        // Message Storages
        StringBuffer messages = new StringBuffer("");


        if (c.moveToFirst()) {

            for (int i = 0; i < c.getCount(); i++) {
                messages.append("################\nFrom:"
                        + c.getString(c.getColumnIndex("address"))
                        + "\nMessage:    "
                        + c.getString(c.getColumnIndex("body"))
                        + "\n#################\n\n");
                c.moveToNext();
            }

          Log.d("X","INBOX:"+messages);

        }

在处理此片段后,名为“messages”的StringBuffer将包含收件箱中的所有邮件。

注意:该类别只能是“收件箱”或“已发送”或“发件箱” 注意:您只需添加
  在清单中