2010-09-07 48 views
0

開發報警應用程序。從BroadCastReceiver.onReceive訪問活動

我在活動上使用listview來保留警報。 應用完成後BroadcastReceiver.onReceive()方法, 我想刪除列表的檢查。

但我不知道如何訪問活動。 有人知道嗎?

下面是我的代碼:

public class Activity_001 extends ListActivity { 

     Intent intent = new Intent(this, ReceiverGenerateAlarm.class); 
     intent.setAction(Conf.GenerateAlarm); 
     intent.putExtra(cal,timerList.get(0).getCal().getTimeInMillis()) 

     PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 
0); 

     AlarmManager am = (AlarmManager) 
(this.getSystemService(ALARM_SERVICE)); 

     am.set(AlarmManager.RTC_WAKEUP, 
timerList.get(0).getCal().getTimeInMillis(), sender); 

} 

public class ReceiverGenerateAlarm extends BroadcastReceiver { 

     @Override 
     public void onReceive(Context context,Intent intent) { 

       String action = intent.getAction(); 
       if(action.equals(Conf.GenerateAlarm)) { 

         Bundle bundle=intent.getExtras(); 
         long cal = bundle.getLongArray("cal"); 

         Calendar c = Calendar.getInstance(); 
         c.setTimeInMillis(cal); 

         MediaPlayer_inherit_Class tm = new MediaPlayer_inherit_Class(cal); 
         tm.play(); 

         //in here, wanna access alarm reservation list and remove check of list. as application has executed. 
         //set next alarm, if needed. 
       } 

回答

1

如果我正確理解你,你應該使用SQLite數據庫來存儲你的「警報保留」。如果這是真的,只需更新您的數據庫條目onReceive()。當Activity恢復時,它應檢查警報數據庫表的更改並相應地更新UI。

另一種方法是從onReceive()bundled extras聯繫startActivity()。可能傳遞行號或時間戳以Intent.putExtra()進行查詢。

另一種方法是將BroadcastReceiver放入您的Activity類中。

希望有所幫助。

+0

每次調用startActivity()都會產生新的活動? 因爲我想要現有的活動的ListView並想更新,我沒有使用startActivity()。而不是它,我把BroadCastReceiver放在我的Activity中。 現在應用程序工作正常。非常感謝你 ! – Yamachan 2010-09-08 19:29:10

+0

很高興幫助! BTW - startActivity()僅在某些情況下生成「新」活動。例如,如果您設置launchMode =「singleInstance」,則現有活動將被置於前面,並且onNewIntent()將被觸發。如果它解決了你的問題,請記住接受這個答案。 – 2010-09-08 20:03:50

+0

爲什麼ans沒有投票或接受?感謝幫助 – Rasel 2015-08-16 10:46:51