2012-08-06 88 views
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 
     }); 
    }       
} 

回答

1

建,你也可以在沒有擴展列表視圖的活動中設置列表視圖。請參閱example。根據我

2

簡單的解決方法是:

  • 1存儲所有在列表中的類,它與Intent延伸BroadcastReceiver

  • 2遍列表,擴展ListActivity

  • 類項目
+0

@ kin-你應該總是接受一個正確的答案,因爲它可以幫助其他有類似問題的人,並檢查這個問題! – 2012-09-28 13:12:41

相關問題