0

enter image description here

When I send a message to another emulator, I can successfully receive the SMS. But there is an additional line showing. How do I remove the 2nd line? It will be helpful if the code is updated using the given codes below.

Inbox Class

  public class Inbox extends Activity implements OnClickListener, OnItemClickListener
  {
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.inbox);
    this.findViewById( R.id.btn_updateSMS ).setOnClickListener( this );
}

   ArrayList<String> msgList = new ArrayList<String>();

public void onItemClick( AdapterView<?> parent, View view, int pos, long id ) 
{

}

public void onClick( View v ) 
{

    Cursor cursor = getContentResolver().query( Uri.parse( "content://sms/inbox" ), null, null, null, null);

    int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
    int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );

    if ( indexBody < 0 || !cursor.moveToFirst() ) return;

    msgList.clear();

    do
    {
        String str = "Sender: " + cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
        msgList.add( str );
    }
    while( cursor.moveToNext() );

    ListView smsListView = (ListView) findViewById( R.id.msgList );
    smsListView.setAdapter( new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, msgList) );
    smsListView.setOnItemClickListener( this );
}
 }

SMS Receiver Class

  public class SmsReceiver extends BroadcastReceiver 
  {

  public static final String ADDRESS = "address";

  public static final String BODY = "body";

  private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
  private static final String TAG = "SMSBroadcastReceiver";

public void onReceive( Context context, Intent intent ) 
{

    if (intent.getAction() == SMS_RECEIVED) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[])bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            }
            if (messages.length > -1) {
                Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
            }
        }
    }
   }
Riyas2329
  • 43
  • 3
  • 14

3 Answers3

0

Not really getting your problem, but why obvious approach of just removing it yourself is an issue? Just look for (most likely) new line character and remove that portion from start of the message to that position.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

remove the \n in here :

 do
    {
        String str = "Sender: " + cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
        msgList.add( str );
    }
    while( cursor.moveToNext() );

use <br> html tag instead. Make sure to resolve the String via fromHtml() function :

something like : textView.setText(Html.fromHtml(str));

also avoid other special characters like an apostrophe(')..

Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

When you fill the sms list, just add a counter i=1, and each time you add one sms to the list, increment it. You will add the sms only if i%2!=0.

In my example:

public List<String> getSMS() {      
    List<String> list = new ArrayList<String>();
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor c = null;
    try{
        c = getApplicationContext().getContentResolver().query(uri, null, null ,null,null); 
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        int i=1;
        for (boolean hasData = c.moveToFirst(); hasData; hasData = c.moveToNext()) {
            final String address = c.getString(c.getColumnIndex("address"));
            final String body = c.getString(c.getColumnIndexOrThrow("body"));
            //Android cripta i messaggi, quindi non devo aggiungerli alla lista:
            if(i%2!=0)
                list.add("Number: " + address + " .Message: " + body);
            i++;
        }
    }catch(Exception e){
        e.printStackTrace();
    }

    // c.close(); 
    return list;
}
Stephan
  • 41,764
  • 65
  • 238
  • 329
Meesta
  • 79
  • 8