不接收新短信

时间:2013-06-25 12:33:54

标签: android

当短信发送给我时。在onatce of BroatcastReceiver中,我调用方法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;
}

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

1 个答案:

答案 0 :(得分: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;
}