当我向另一个模拟器发送消息时,我可以成功接收短信。但是还有一条额外的线显示。如何删除第二行?如果使用下面给定的代码更新代码将会很有帮助。
收件箱类
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());
}
}
}
}