0

When a SMS send to me. In onReceive of the BroatcastReceiver, I call method 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;
}

But it return previous SMS of the new SMS, not new SMS. How to fix this error?

Ken
  • 1
  • 2

1 Answers1

0

The easiest way to do that would probably be to register your BroadcastReceiver to the android.provider.Telephony.SMS_RECEIVED action and get the SMS message from the intent received:

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;
}
Muzikant
  • 8,070
  • 5
  • 54
  • 88
  • This thing which I know. But I need **its ID** in order to I can delete it from my List SMS. – Ken Jun 25 '13 at 14:06
  • In that case, the broadcast action is not what you are looking for. Try using ContentObserver for SMS as described in this question: http://stackoverflow.com/questions/11281533/contentobserver-used-for-sms – Muzikant Jun 27 '13 at 17:24