简体   繁体   中英

android: AlarmManager and activity life cycle

I decided to write simple alarm clock (using AlarmManager). When alarm works out I want to show simple dialog with 2 buttons: OK and Snooze. So I have a question: what should I connect with my Pending Intent? I mean Service, Activity or BroadcastReceiver? At present version I use BroadcastReceiver where I start Activity that shows the dialog. I start it with flag FLAG_ACTIVITY_NEW_TASK. In onStrat() method I start music service. When OK button's pressed I call finish() for activity and stop music. In OnPause() I call finish() also. I do it because if two alarms run simultaneously then according to activity life cycle method onPause() will be called. It works... but sometime the music starts for a few seconds then finishes....then start again and finishes and so on. Why? Thanks.

So I have a question: what should I connect with my Pending Intent? I mean Service, Activity or BroadcastReceiver?

Probably an activity in this case. Theme your activity to look the way you want (eg, Theme.Dialog ) rather than fussing around with a regular dialog box.

It works... but sometime the music starts for a few seconds then finishes....then start again and finishes and so on. Why?

It is impossible to answer this question with the information you have supplied, sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM