2

I am sure I must just be missing something obvious but this is currently baffling me.

I have a class which extends BroadcastReceiver this class is setup as a receiver int he manifest and it does correctly capture the sms received intent.

package gull.sana.textit;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("SMSReceiver", "Intent received");
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        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]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }
            Log.d("SMS", str);
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

        }
    }
}

Now when I send in an SMS message I always get the first log message Log.d("SMSReceiver", "Intent received"); and I always get the Toast as I would expect, however, I never get the Log.d("SMS", str);

Am I doing something wrong? Why does the Toast appear yet the log on the line before never does?

Jerry101
  • 12,157
  • 5
  • 44
  • 63
Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • Do you see either of the `Log` statements? Are you positive it's not there? Are you pulling log files from the wrong device maybe? – PearsonArtPhoto Feb 27 '14 at 22:19
  • Are you sure you aren't filtering for "SMSReceiver" instead of just "SMS"? – JRomero Feb 27 '14 at 22:21
  • Yes it is definitely the correct (and only android device), and I always get the first Log message but never the second. The if statement is definitely entered as the Toast always appears too. – Jon Taylor Feb 27 '14 at 22:21
  • I have no filters set on logcat as I can see all other log messages from the phone. – Jon Taylor Feb 27 '14 at 22:21
  • Clean, Compile again, Log.d("SMSReceiver", str); And run. if not marking this as my favorite question to see the answer :D – MDMalik Feb 27 '14 at 22:29

2 Answers2

4

It turns out that there are several buffers for log messages. The tags decide which buffer these logs go into. According to this https://stackoverflow.com/a/9011945/1475461 SMS is one of those tags that gets put into a different buffer.

The other buffer can be viewed with the following command

adb logcat -b radio

Thanks to @nKn for getting me along the right tracks.

Community
  • 1
  • 1
Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
1

This seems to be an old known issue when you use Log.* in combination with a tag called SMS. There seems not logical explaination to this but your code is working accordingly, it's just the Log.d() line that is not working correctly.

You should replace the tag SMS with some other, in that case it should work as expected (in this case, avoiding the bug).

nKn
  • 13,691
  • 9
  • 45
  • 62
  • Is this bug reported anywhere? – Jon Taylor Mar 03 '14 at 00:17
  • Ah infact, from your answer I have found another answer http://stackoverflow.com/questions/8699756/logcat-not-displaying-tag-sms which explains that the issue is not a bug, but seperate buffers into which the logs go. – Jon Taylor Mar 03 '14 at 00:19
  • I remembered I had read similar questions to yours several times, not I've tried looking for an official bug but no luck, seems that the question you just found is the most reasonable explaination for this. – nKn Mar 03 '14 at 00:22
  • 1
    Thanks, it helped me out suggesting that SMS specifically does not work. – Jon Taylor Mar 03 '14 at 00:23