我有以下代码来显示列表视图中收到的消息:
package com.example.smsTest;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SMSReceiverActivity extends ListActivity {
private BroadcastReceiver mIntentReceiver;
ListView listview;
ArrayAdapter<String> arrayAdpt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsreceiver);
listview=this.getListView();
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("get_msg");
//Process the sms format and extract body & phoneNumber
msg = msg.replace("\n", "");
String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
String pNumber = msg.substring(0,msg.lastIndexOf(":"));
//Add it to the list or do whatever you wish to
ArrayList<String> bodyarr=new ArrayList<String>();
bodyarr.add(body);
arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
bodyarr);
listview.setAdapter(arrayAdpt);
arrayAdpt.notifyDataSetChanged();
}
};
this.registerReceiver(mIntentReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
this.unregisterReceiver(this.mIntentReceiver);
}
}
但是,问题是之前的消息被覆盖了。我尝试添加arrayAdpt.notifyDataSetChanged();
代码无济于事。
我也在这里阅读了很多答案,但它不适用于我的代码。
请提供任何帮助。