Search code examples
androidstringsmssmsmanager

How get the sms Body for a message tracking?


I want to get the message in to a string and split the message. But I cannot get the message represent by "body", in to a string. Please tell me if you know how to put message body in to a string. This is my sample code,

 protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fulldetail);

    lst=(ListView)findViewById(R.id.rldlist1);

    Uri in = Uri.parse("content://sms/inbox");
    ContentResolver cr = getContentResolver();
    Cursor c = cr.query(in, new String[]{"_id", "address", "body"}, "address = 'eZ Reload'", null, null);
    adapter = new SimpleCursorAdapter(this, R.layout.rowsms, c, new String[]
            {"address", "body"}, new int[]{R.id.lblreloadbody, R.id.lblreloadno});

    lst.setAdapter(adapter)

Solution

  • You can use String body = sms.getMessageBody().toString(); take a look to this code:

            // Get the SMS map from Intent
            Bundle extras = intent.getExtras();
             
            String messages = "";
             
            if ( extras != null )
            {
                // Get received SMS array
                Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
                 
                // Get ContentResolver object for pushing encrypted SMS to the incoming folder
                ContentResolver contentResolver = context.getContentResolver();
                 
                for ( int i = 0; i < smsExtra.length; ++i )
                {
                    SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
                     
                    String body = sms.getMessageBody().toString();
                    String address = sms.getOriginatingAddress();
                     
                    messages += "SMS from " + address + " :\n";                    
                    messages += body + "\n";
                     
                    // Here you can add any your code to work with incoming SMS
                    // I added encrypting of all received SMS 
                     
                    putSmsToDatabase( contentResolver, sms );
                }
                 
                // Display SMS message
                Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
            }
             
    

    Read more at:

    http://www.apriorit.com/dev-blog/227-handle-sms-on-android

    How to read the message content of a new in coming message in android?