1

这个项目需要两个类。

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

2 回答 2

2

根据我的最简单的解决方案是:

  • 1 将所有项目存储在扩展类中的列表中BroadcastReceiver

  • 2 将列表传递Intent给扩展的类ListActivity

于 2012-08-06T05:53:21.340 回答
1

Kin,您也可以在Activity中设置listview而不扩展Listview。请参阅此示例

于 2012-08-06T05:51:12.817 回答