【问题标题】:Getting phone number of each sms via content://sms/通过 content://sms/ 获取每条短信的电话号码
【发布时间】:2012-08-31 20:05:01
【问题描述】:

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

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();
            }

        }

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

【问题讨论】:

    标签: android sms


    【解决方案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;
    
                 }
    

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

    【讨论】:

    • 一旦我设法对此进行测试,我将标记为答案。感谢回复
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2011-07-30
    相关资源
    最近更新 更多