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.
}
每次調用startActivity()都會產生新的活動? 因爲我想要現有的活動的ListView並想更新,我沒有使用startActivity()。而不是它,我把BroadCastReceiver放在我的Activity中。 現在應用程序工作正常。非常感謝你 ! – Yamachan 2010-09-08 19:29:10
很高興幫助! BTW - startActivity()僅在某些情況下生成「新」活動。例如,如果您設置launchMode =「singleInstance」,則現有活動將被置於前面,並且onNewIntent()將被觸發。如果它解決了你的問題,請記住接受這個答案。 – 2010-09-08 20:03:50
爲什麼ans沒有投票或接受?感謝幫助 – Rasel 2015-08-16 10:46:51