【发布时间】:2017-10-08 04:01:03
【问题描述】:
我的闹钟代码运行良好,因为默认铃声可以按时正常播放。但无法停止警报音。在接收器类中,我插入了以下代码。
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
我还在主类中插入了取消警报的代码,代码也很好用
alarmMgr.cancel(pendingIntent);
但是一旦默认铃声开始播放并且用户想通过停止按钮停止它,它就不起作用了。 在带有 stopbutton.setOnClickListener 的主类中,我插入了以下代码
Intent intent = new Intent(getBaseContext(), Reciver.class);
stopService(intent);
我认为可以通过 r.stop(); 但是我应该如何在停止按钮中使用它。
【问题讨论】:
-
不准确,如果我没记错的话,您将停止按钮放在自己的 UI 上,但它不起作用,我说的对吗?
-
我在主布局中有三个按钮与主类连接。开始按钮调用警报管理器并唤醒接收器类(r.play 在这个类中)并且运行良好。取消按钮取消警报。现在第三个按钮是停止按钮。