我已经创建了一个闹钟应用作为我的第一个 kotlin 个人项目,但我在使用“请勿打扰”模式时遇到了问题。我创建闹钟的方式是:
使用日期选择器选择闹钟时间/日期。
使用挂起的 Intent 和 AlarmManager 来安排警报,如您在代码中所见。
//2. Get AlarmManager instance
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
//3. Pending Intent
val pendingIntentRequestCode = 10 //Para recuperar el intent
val flag = 0
pendingIntentAlarm = PendingIntent.getBroadcast(this, pendingIntentRequestCode, intent, flag)
// 4. Schedule the alarm
alarmManager!!.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, schedule.timeInMillis, pendingIntentAlarm)
要接收待处理的意图,请运行我使用 BroadcastReceiver 的警报操作:
class AlarmReceiver: BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context, intent: Intent) {
我过去打开闹钟的方式是发出通知。为了在 android 中创建和运行通知,我为 android Oreo 及更高版本创建了通知通道。代码是:
通知渠道创建:
val CHANNEL_ID = "runAlarm"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_runal_name)
val descriptionText = getString(R.string.channel_runal_desc)
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setLegacyStreamType(AudioManager.STREAM_ALARM)
.setUsage(AudioAttributes.USAGE_ALARM)
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.build()
channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM), audioAttributes)
channel.enableLights(true)
channel.enableVibration(true)
channel.lockscreenVisibility = NotificationCompat.VISIBILITY_PUBLIC
channel.setBypassDnd(true)
// Register the channel with the system
notificationManager = (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
notificationManager!!.createNotificationChannel(channel)
通知构建和启动:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notification = Notification.Builder(context, "runAlarm")
.setSmallIcon(R.drawable.alarm_icon)
.setContentTitle("GET UP!!!")
.setCategory(Notification.CATEGORY_ALARM)
.setOngoing(true)
.setFullScreenIntent(fullScreenPendingIntent, true)
.setOnlyAlertOnce(true)
.build()
//Launch notification
val notifId = 2
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(schedNotifId)
notificationManager.notify(notifId, notification)
} else { //NO SUENA PARA SISTEMAS MENORES O IGUALES QUE OREO_MR1 EN MODO DO NOT DISTURB
val notification = NotificationCompat.Builder(context, "runAlarm")
.setSmallIcon(R.drawable.alarm_icon)
.setContentTitle("GET UP!!!")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOngoing(true)
.setFullScreenIntent(fullScreenPendingIntent, true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM), AudioManager.STREAM_ALARM)
.setOnlyAlertOnce(true)
.setDefaults(Notification.DEFAULT_SOUND or NotificationCompat.DEFAULT_VIBRATE)
.build()
//Launch notification
val notifId = 2
val notificationManager = NotificationManagerCompat.from(context)
notification.flags = NotificationCompat.FLAG_INSISTENT
notificationManager.cancel(schedNotifId)
notificationManager.notify(notifId, notification)
但是,使用此配置,通知会出现,但不会在正常模式和免打扰 (DnD) 模式下响铃。
为了尝试修复它,我在通知调用后添加了声音服务。调用服务的代码是:
val startIntent = Intent(context, RingtonePlayingService::class.java)
context.startService(startIntent)
服务描述如下:
class RingtonePlayingService : Service() {
private var ringtone: Ringtone? = null
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
ringtone = RingtoneManager.getRingtone(this, ringtoneUri)
ringtone!!.play()
return START_NOT_STICKY
}
override fun onDestroy() {
ringtone!!.stop()
}
现在,Android Oreo 及更高版本的闹钟在正常和 DnD 模式下响起,在 android < Oreo 中在正常模式下响起,但其音量取决于铃声音量而不是闹钟音量。
但是在 DnD 模式下它不会在 android < Oreo 中发声,并且音量还取决于铃声音量而不是闹钟音量。
我不知道问题是否是因为这是最好的方法,任何帮助将不胜感激。
先谢谢你!