0

我编写了以下代码,以获取用户和数字之间的整个对话:

Uri SMS_INBOX = Uri.parse("content://sms/");
        String selection = "thread_id = " + thread_id;
        final String[] projection = new String[] { "*" };
        Cursor c = getContentResolver().query(SMS_INBOX, projection, selection,null, "date");

        startManagingCursor(c);

        String[] body = new String[c.getCount()];
        String[] address = new String[c.getCount()];
        if (c.moveToFirst()) {
            for (int j = 0; j < c.getColumnCount(); j++)
                Log.w("ColumnName", c.getColumnName(j));
            for (int i = 0; i < c.getCount(); i++) {
                body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();
                address[i] = c.getString(c.getColumnIndexOrThrow("address")).toString();
                Log.d("address-" + i, address[i]);
                Log.d("body-" + i, body[i]);
                String subject =  c.getString(c.getColumnIndexOrThrow("_id")).toString();
                Log.d("_id-" + i, subject);
                String thread =  c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
                Log.d("thread_id-" + i, subject);
                Log.d("----", "----");

                c.moveToNext();
            }

        }

通过此代码,我得到了对话中的所有消息。问题是,我不知道哪个号码正在发送哪个消息。如果我得到“地址”列,它总是返回相同的号码(实际上它只返回对方的号码),所以我无法记录我刚刚通过此代码收到的消息是由用户发送还是另一个号码。

4

1 回答 1

1

该列将始终仅提供第二个人编号。如果要区分已发送消息和已接收消息,则必须使用“类型”列。

body[i] = c.getString(c.getColumnIndexOrThrow("body")).toString();

if(c.getString(c.getColumnIndex("type")).equalsIgnoreCase("1")){

                // sms received

                msg_state[i]=1;

             }

              else {
                //sms sent
                msg_state[i]=0;

             }

否 您可以轻松识别发送的短信和收到的短信。

于 2012-08-31T12:05:01.890 回答