1
該項目需要兩個類。如何在同一代碼中同時使用BroadcastReceiver和ListActivity
BroadcastReceiver
(class) - 接收短信;ListActivity
(class) - 顯示SMS;
因爲setListAdapter()
方法需要定義ListActivity
(類),我的問題是我應該怎麼在下面的代碼定義了兩個類?
import ....
public class SmsReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
}
//----REQUIRE ListActivity(class) to define----//
// how should i define the class here???
//---display in list---
setListAdapter(new ArrayAdapter<String>(this, R.layout.main,str));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
//---method is call when listitem is clicked---
listView.setOnItemClickListener(new OnItemClickListener() {
//described method
});
}
}
@ kin-你應該總是接受一個正確的答案,因爲它可以幫助其他有類似問題的人,並檢查這個問題! – 2012-09-28 13:12:41