我已经尝试过类似的问题阻止警报在另一个活动中响起。但是,它在我的情况下不起作用。我想要实现的是:
- 当通知触发时,铃声在广播接收器中开始。
- 如果设备处于开启状态,用户可以在弹出通知上停止铃声按下按钮。
- 如果设备关闭,则全屏活动打开,用户可以在此活动中按停止按钮停止铃声。
问题是,当我在全屏活动中按下停止按钮时,铃声不会停止。
这是代码。报警接收器:
class AlarmReceiver: BroadcastReceiver() {
companion object {
lateinit var ringtoneSound: Ringtone
val ALARM_CHANNEL_ID = "alarm_channel"
}
lateinit var notificationManager: NotificationManager
override fun onReceive(context: Context, intent: Intent) {
val alarmId = intent.getIntExtra("alarmId", 0)
val title = intent.getStringExtra("title")
val memo = intent.getStringExtra("memo")
val ringLength = intent.getIntExtra("ringLength", 5)
notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createAlarmChannel()
val contentIntent = Intent(context, MainActivity::class.java)
val contentPendingIntent = PendingIntent.getActivity(
context, alarmId, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val fullScreenIntent = Intent(context, LockscreenActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(context, alarmId, fullScreenIntent, 0)
val deleteIntent = Intent(context, StopAlarmReceiver::class.java).putExtra("alarmId", alarmId)
val deletePendingIntent = PendingIntent.getBroadcast(context,alarmId,deleteIntent,0)
val builder = NotificationCompat.Builder(context, ALARM_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_circle_24)
.setContentTitle(title)
.setContentText(memo)
.setColor(ResourcesCompat.getColor(context.resources, R.color.purple_700, null))
.setContentIntent(contentPendingIntent)
.setFullScreenIntent(fullScreenPendingIntent, true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setAutoCancel(true)
.setSound(null)
.addAction(R.drawable.ic_circle_24, "STOP", deletePendingIntent)
notificationManager.notify(alarmId, builder.build())
// when notification fired, Ringtone starts.
ringtoneSound = RingtoneManager.getRingtone(context, getDefaultUri(RingtoneManager.TYPE_ALARM))
ringtoneSound.play()
// and after certain seconds, Ringtone will stop.
val countLength = 1000 * ringLength.toLong()
object : CountDownTimer(countLength, 1000) {
override fun onTick(millisUntilFinished: Long) {}
override fun onFinish() {
ringtoneSound.stop()
notificationManager.cancel(alarmId)
}
}.start()
}
fun createAlarmChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val alarmChannel = NotificationChannel(ALARM_CHANNEL_ID, "Alarm", NotificationManager.IMPORTANCE_HIGH)
alarmChannel.setSound(null, null)
alarmChannel.enableLights(true)
alarmChannel.lightColor = Color.RED
alarmChannel.enableVibration(true)
alarmChannel.description = "Alarm style"
alarmChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(alarmChannel)
}
}
}
停止警报接收器:
class StopAlarmReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val alarmId = intent.getIntExtra("alarmId", 0)
// This stops alarm when press the button of notification.(It works!)
AlarmReceiver.ringtoneSound.stop()
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(alarmId)
}
}
LockScreenActivity:
class LockscreenActivity : AppCompatActivity() {
private lateinit var binding: ActivityLockscreenBinding
override fun onCreate(savedInstanceState: Bundle?) {
showWhenLockedAndTurnScreenOn()
super.onCreate(savedInstanceState)
binding = ActivityLockscreenBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.stoppingbutton.setOnClickListener {
// This does not stop Alarm!
AlarmReceiver.ringtoneSound.stop()
finish()
}
}
private fun showWhenLockedAndTurnScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true)
setTurnScreenOn(true)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
)
}
}
}
StopAlarmReceiver 正在工作,但是即使使用相同的代码,LockScreenActivity 也不起作用。
当然,如果我写start()
在 LockScreenActivity 上,它就可以工作。但是,当设备打开并出现弹出窗口时,铃声不会开始。那么如何从 Activity 中停止接收者的铃声呢?任何帮助表示赞赏。