根据电话号码查询短信对话,无需国家/地区代码

时间:2014-09-17 12:48:44

标签: android sms android-contentresolver

这是我的代码段:

void getAllMessages() {
        if (mValidPhoneNumberString != "") {
            final String SMS_URI_INBOX = "content://sms/inbox";
            int aInt_Thread = 0;
            try {
                Uri aUri = Uri.parse(SMS_URI_INBOX);
                String[] aProjection = new String[] { "address", "person",
                        "body", "date", "type", "thread_id" };
                Cursor aCursor = getContentResolver().query(aUri, aProjection,
                        "address=" + "'" + mValidPhoneNumberString + "'", null,
                        "date desc");
                if (aCursor.moveToFirst()) {
                    int aIndex_thread = aCursor.getColumnIndex("thread_id");
                    aInt_Thread = aCursor.getInt(aIndex_thread);
                }

                if (aCursor != null) {
                    Uri SMS_INBOX = Uri.parse("content://sms");
                    Cursor aCur = getContentResolver().query(
                            SMS_INBOX,
                            new String[] { "_id", "address", "person", "body",
                                    "date", "type", "thread_id" },
                            "thread_id" + " = " + aInt_Thread, null,
                            "date" + " ASC");

                    int index_Body = aCur.getColumnIndex("body");
                    int index_Date = aCur.getColumnIndex("date");
                    int index_Type = aCur.getColumnIndex("type");
                    while (aCur.moveToNext()) {

                        String aMessageBody = aCur.getString(index_Body);
                        int aType = aCur.getInt(index_Type);
                        String aMessageNumber = "";
                        try {
                            aMessageNumber = aCur.getString(
                                    aCur.getColumnIndexOrThrow("address"))
                                    .toString();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        long aMessageTimeStamp = aCur.getLong(index_Date);
                        Constants.logInfo("Address", "" + aMessageNumber);
                        Constants.logInfo("BODY", aMessageBody);
                        Constants.logInfo("Type", "" + aType);
                        Constants.logInfo("Date", ""
                                + getFormattedDate(aMessageTimeStamp));
                    }
                    if (!aCur.isClosed()) {
                        aCur.close();
                        aCur = null;
                    }
                }
                if (!aCursor.isClosed()) {
                    aCursor.close();
                    aCursor = null;
                }

            } catch (SQLiteException ex) {
                Constants.logDebug("SQLiteException", ex.getMessage());
            }
        }

    }

这里" mValidPhoneNumberString"是没有国家/地区代码的电话号码。 现在发生的情况是,当我调用此方法时,它不会返回任何消息,但是当我将国家/地区代码添加到提供的号码时,它会显示与电话号码相关的所有会话。 我真的很想知道其他人是否早些时候遇到过同样的问题,他们是如何解决的。

0 个答案:

没有答案