0

在此处输入图像描述

当我向另一个模拟器发送消息时,我可以成功接收短信。但是还有一条额外的线显示。如何删除第二行?如果使用下面给定的代码更新代码将会很有帮助。

收件箱类

  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 );
}
 }

短信接收类

  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());
            }
        }
    }
   }
4

3 回答 3

0

当您填写短信列表时,只需添加一个计数器i=1,每次向列表中添加一条短信时,将其递增。只有在i%2!=0.

在我的例子中:

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;
}
于 2012-10-16T14:57:46.510 回答
0

并没有真正解决您的问题,但为什么只是自己删除它的明显方法是一个问题?只需寻找(最有可能的)换行符并将该部分从消息的开头删除到该位置。

于 2012-07-29T11:09:33.803 回答
0

\n在这里删除:

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

改用<br>html 标签。确保通过fromHtml()函数解析字符串:

就像是 :textView.setText(Html.fromHtml(str));

还要避免其他特殊字符,如撇号(')..

于 2012-07-29T12:32:56.563 回答