无法停止其他活动的响铃警报 [英] Can't stop the ringing alarm from another activity
问题描述
我是android的新手,这里我正在练习我的第一个应用程序(警报应用程序)。
I am new to android ,Here I am practicing my first app (Alarm App).
我的应用程序中存在一个问题,我无法停止警报一旦触发,警报就不会停止。
I have an issue in my app that I can't stop the alarm once a alarm is triggered it's keep on ringing can't get stopped.
在我的应用中,我有2个活动和一个AlarmReceiver。
In my app I have 2 activities and a AlarmReceiver .
在AlarmActivity.java中,我已经设置了警报,到达特定时间后,Alarmreceiver.java将被触发,警报开始响起并显示唤醒屏幕。
From the AlarmActivity.java I have set the alarm ,when the specific time is reached the Alarmreceiver.java will get triggered and the alarm started to ring and showing a wakeup screen .
在WakeUpScreen.java中,我有一个停止按钮,需要停止当前的响铃警报。
From the WakeUpScreen.java I have a stop button by using that I need to stop the current ringing alarm .
我没有
AlarmActivity.java
AlarmActivity.java
public void stopAlarm(Context context) {
Intent intent = new Intent(context,AlarmReceiver.class);
intent.setAction("ALARM_OFF");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mAlarmId, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
我在AlarmActivity.java中有这个AlarmStop()函数,它将被点击当我按下wakeupscreen.java中的停止按钮时。
I have this AlarmStop() function in my AlarmActivity.java which will get hit when I press the stop button in wakeupscreen.java
当我尝试调试停止过程时,调试器将stopAlarm()函数中的所有行都移了,但是Intent不起作用,这就是为什么没有调用AlarmReceiver.java文件的原因,我的意思是,调试器移动了上述方法中的所有行,但没有调用AlarmReceiver.java
我尝试了很多方法来解决此问题,但我错过了一些我无法解决的问题。
I tried lot of ways to solve this but I missed something that I can't figured it out.
有人可以帮我停下来吗?
Can anyone help me to stop the triggered alarm and it's ringing sound .
推荐答案
您的体系结构已损坏。您无需使用 BroadcastReceiver
进行持久处理。 BroadcastReceiver
的生命周期很短,您可以用它触发其他事情。
Your architecture is broken. You don't use a BroadcastReceiver
for persistent processing. A BroadcastReceiver
has a very short lifecycle, you use it to trigger other things.
您已经创建了<您的 BroadcastReceiver
中的code> MediaPlayer 实例,并试图在 onReceive()$ c中控制它$ c>。错了您应该使用
服务
来管理和维护 MediaPlayer
的状态。
You've created a MediaPlayer
instance in your BroadcastReceiver
and are trying to control that in onReceive()
. This is wrong. You should use a Service
to manage and maintain the state of your MediaPlayer
.
看看是否可以在Internet上找到有关如何构建这样的应用程序的HOWTO指南。
See if you can find some HOWTO guides on the Internet for how to build such an application.
这篇关于无法停止其他活动的响铃警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!