我的项目用于接收短信并显示在列表中。以下代码行发生错误
setListAdapter(new ArrayAdapter<String>(this, R.layout.main,str));
由于 ListActivity 未在下面的代码中定义。但是,接收 SMS 的方法需要 BroadcastReceiver。
public class SmsReceiver extends BroadcastReceiver{
那么,问题是如何在一个 java 代码中定义两个超类?完整的java代码如下:
package net.eg.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.ListActivity;
import android.telephony.SmsMessage;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
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();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---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
});
}
}
}