简体   繁体   English

android:AlarmManager和活动生命周期

[英]android: AlarmManager and activity life cycle

I decided to write simple alarm clock (using AlarmManager). 我决定编写简单的闹钟(使用AlarmManager)。 When alarm works out I want to show simple dialog with 2 buttons: OK and Snooze. 当闹钟响起时,我想显示一个带有2个按钮的简单对话框:“确定”和“暂停”。 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. 在当前版本中,我使用BroadcastReceiver,在其中启动显示对话框的Activity。 I start it with flag FLAG_ACTIVITY_NEW_TASK. 我从标志FLAG_ACTIVITY_NEW_TASK开始。 In onStrat() method I start music service. 在onStrat()方法中,我启动音乐服务。 When OK button's pressed I call finish() for activity and stop music. 当按下“确定”按钮时,我调用finish()进行活动并停止音乐。 In OnPause() I call finish() also. 在OnPause()中,我也调用finish()。 I do it because if two alarms run simultaneously then according to activity life cycle method onPause() will be called. 我这样做是因为,如果两个警报同时运行,那么将根据活动生命周期方法调用onPause()。 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. 为您的活动设置主题以使其看起来像您想要的方式(例如Theme.Dialog ),而不是Theme.Dialog常规对话框。

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. 很抱歉,无法使用您提供的信息来回答这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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