来自同一号码的群组短信

时间:2014-05-07 10:41:48

标签: android

我正在尝试构建自己的短信应用程序(仅用于学习目的)。现在,当我收到短信时,我将其保存为:

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
long time_rcv = currentMessage.getTimestampMillis();

ContentValues smsValues = new ContentValues();
smsValues.put("address", phoneNumber);
smsValues.put("body", message);
smsValues.put("date_sent", time_rcv);
context.getContentResolver().insert(BlacklistConstants.smsInboxUri,smsValues);

除非我想显示消息,否则这样可以正常工作:

来自相同编号的那些单独显示。

当我像这样检索短信时:

Uri uri = Uri.parse(String.valueOf(BlacklistConstants.smsConversations));
Cursor c = getActivity().getContentResolver().query(uri, null, null, null, null);

我得到了输出:

msg count: 1
thread_id: 0
Snippet: 2

msg count: 1
thread_id: 0
Snippet: 2

msg count: 1
thread_id: 0
Snippet: 2

msg count: 1
thread_id: 0
Snippet: 2

这样的消息:

从100开始 从11开始 从12点开始 从12开始

所以实际上我应该得到一条有2条消息。

有人能指出我如何解决这个问题吗?

编辑:

这就是我获取短信的方式。 displayConversations仅供观看:

 // Fills the List with Smss
    private void fillSmsList() {
    smsList = new ArrayList<SMSData>();

    Uri uri = Uri.parse(String.valueOf(BlacklistConstants.smsInboxUri));
    Cursor c = getActivity().getContentResolver().query(uri, null, null, null, null);

    if (c.moveToFirst()) {
        for (int i = 0; i < c.getCount(); i++) {
            SMSData sms = new SMSData();
            sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
            sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
            sms.setId(c.getString(c.getColumnIndexOrThrow("_id")).toString());
            sms.setTimeStampReceived(c.getString(c.getColumnIndexOrThrow("date_sent")).toString());
            smsList.add(sms);
            c.moveToNext();
        }
    }
    c.close();
}

private void displaySmsConversations() {
    Log.d(TAG,"DisplaySmsConverstations");
    Uri uri = Uri.parse(String.valueOf(BlacklistConstants.smsConversations));
    Cursor c = getActivity().getContentResolver().query(uri, null, null, null, null);

    if (c.moveToFirst()) {
        for (int i = 0; i < c.getCount(); i++) {
            Log.d(TAG,"msg count: " + c.getColumnIndexOrThrow("msg_count"));
            Log.d(TAG,"thread_id: " + c.getColumnIndexOrThrow("thread_id"));
            Log.d(TAG,"Snippet: " + c.getColumnIndexOrThrow("snippet"));
            c.moveToNext();
        }
    }
    c.close();
}

1 个答案:

答案 0 :(得分:1)

您应该使用BroadcastReceiver#onReceive方法连接多部分消息中的消息,如下所示:

Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}

if (messages.length < 0) {
    return;
}

SmsMessage sms = messages[0];
String body = "";
String sender = sms.getOriginatingAddress().toString();

try {
    if (messages.length == 1 || sms.isReplace()) {
        body = sms.getDisplayMessageBody();
    } else {
        StringBuilder bodyText = new StringBuilder();
        for (int i = 0; i < messages.length; i++) {
            bodyText.append(messages[i].getMessageBody());
        }
        body = bodyText.toString();
    }
} catch (Exception e) {

}

ContentValues smsValues = new ContentValues();
smsValues.put("address", sender);
smsValues.put("body", body);
smsValues.put("date_sent", time_rcv);
context.getContentResolver().insert(BlacklistConstants.smsInboxUri,smsValues);