0

当短信发送给我时。在BroatcastReceiver的onReceive中,我调用了readNewSMS()方法:

private MySMS readNewSMS(String phoneNumber) {
    MySMS sms = new MySMS();

    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cursor = getSherlockActivity().managedQuery(uri, null,
            null, null, null);
    cursor.moveToFirst();

        sms.setNumber(cursor.getString(
                cursor.getColumnIndexOrThrow("address")).toString());
        sms.setId(cursor.getString(
                cursor.getColumnIndexOrThrow("_id")).toString());
        sms.setThread_id(cursor.getString(cursor
                .getColumnIndexOrThrow("thread_id")));
        sms.setBody(cursor.getString(
                cursor.getColumnIndexOrThrow("body")).toString());
        sms.setTime(cursor.getString(
                cursor.getColumnIndexOrThrow("date")).toString());
        sms.setType(cursor.getString(
                cursor.getColumnIndexOrThrow("type")).toString());

    return sms;
}

但它返回新短信的先前短信,而不是新短信。如何修复此错误?

4

1 回答 1

0

最简单的方法可能是将您的 BroadcastReceiver 注册到android.provider.Telephony.SMS_RECEIVED操作并从收到的意图中获取 SMS 消息:

protected SmsMessage[] extractSmsMessagesFromIntent(Intent p_intent) {
    //---get the SMS message passed in---
    Bundle bundle = p_intent.getExtras();        
    SmsMessage[] msgs = null;
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i = 0; i < msgs.length; i++)
        {
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
        }
    }
    return msgs;
}
于 2013-06-25T13:30:22.743 回答