如何在Android工作室Kotlin中添加多个警报?

fnatzsnv  于 2023-03-30  发布在  Kotlin
关注(0)|答案(1)|浏览(175)

我正在尝试向我的应用程序添加触发通知的闹钟;问题是,每当我添加一个新的警报,前一个不工作。
我想这是因为传递的请求代码(所有报警都有相同的代码(0)),但我不知道如何每次传递一个新的请求代码。
这是我的setAlarm()函数:

private fun setAlarm() {
        alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
        val intent = Intent(this, AlarmReceiver::class.java)
        pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

        alarmManager.setRepeating(
            AlarmManager.RTC_WAKEUP,
            calender.timeInMillis,
            AlarmManager.INTERVAL_DAY,
            pendingIntent
        )
        Toast.makeText(this, "alarm set", Toast.LENGTH_SHORT).show()

    }

这是我的警报接收器类:

private lateinit var pendingIntent: PendingIntent
    override fun onReceive(p0: Context?, p1: Intent?) {

        val intent = Intent(p0, MainActivity::class.java)
        p1!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        pendingIntent = PendingIntent.getActivity(p0, 0, intent, 0)

        val builder = NotificationCompat.Builder(p0!!, "androidAlarm")
            .setSmallIcon(drawable.notification_bg)
            .setContentTitle("Your reminder!")
            .setContentText("Your work has to be done now!")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setContentIntent(pendingIntent)

        val notificationManager = NotificationManagerCompat.from(p0)
        if (ActivityCompat.checkSelfPermission(
                p0,
                Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return
        }
        notificationManager.notify(123, builder.build())
    }

}

我也不知道如果用户想重复警报,我如何重复警报。

我尝试在请求代码参数中使用System.currentTimeMillis(),但是没有用。

我找了很多,但没有找到很多

这是我在尝试System.currentTimeMillis()时使用的代码:

private fun setAlarm() {
    calender = Calendar.getInstance()
    alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager

    val intent = Intent(this, AlarmReceiver::class.java)
    pendingIntent = PendingIntent.getBroadcast(this, System.currentTimeMillis().toInt(), intent, 0)
    alarmManager.setRepeating(
        AlarmManager.RTC_WAKEUP,
        calender.timeInMillis,
        AlarmManager.INTERVAL_DAY,
        pendingIntent)
    Toast.makeText(this, "alarm set", Toast.LENGTH_SHORT).show()

}

这是接收器类:

class AlarmReceiver : BroadcastReceiver() {
private lateinit var pendingIntent: PendingIntent
override fun onReceive(p0: Context?, p1: Intent?) {
    val id = System.currentTimeMillis().toInt()
    val intent = Intent(p0, MainActivity::class.java)
    p1!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    pendingIntent = PendingIntent.getBroadcast(p0, System.currentTimeMillis().toInt(), intent, 0)

    val builder = NotificationCompat.Builder(p0!!, "androidAlarm")
        .setSmallIcon(drawable.notification_bg)
        .setContentTitle("Your reminder!")
        .setContentText("Your work has to be done now!")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setDefaults(NotificationCompat.DEFAULT_ALL)
        .setContentIntent(pendingIntent)

    val notificationManager = NotificationManagerCompat.from(p0)
    if (ActivityCompat.checkSelfPermission(
            p0,
            Manifest.permission.POST_NOTIFICATIONS
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return
    }
    notificationManager.notify(123, builder.build())
    Log.e("notification: ", "received a notification: ${pendingIntent.toString()}")
}

}

  • 我正在使用setRepeating,因为set和setExact不起作用,只要我添加它,它们就会触发警报,而不管它设置的时间如何。*
    例如,我的主要目标是能够创建多个不同时间触发的闹钟,并且如果用户愿意,还能够每天重复某个闹钟
ruoxqz4g

ruoxqz4g1#

在使用Alarm时,必须明确一些概念。

设置多个告警

为了有不同的报警,在创建PendingIntent时,您可以有不同的requestCode

public static PendingIntent getBroadcast (Context context, 
                int requestCode, 
                Intent intent, 
                int flags)

例如,您应该使用以下两个不同的PendingIntent:

val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntentFirst = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntentSecond = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT)

而除了requestCode可以识别不同的PendingIntent之外,还有更多的属性可以比较2个PendingIntent是否相等,具体可以参考here

AlarmManager.setRepeating()不精确

从API 19开始(应该是Android 4.4),AlarmManager.setRepeating()设置的闹钟不准确:
注:自API 19起,所有重复的警报都是不精确的。如果您的应用程序需要精确的传递时间,则必须使用一次性精确警报,并按上述方法重新安排每次。targetSdkVersion早于API 19的旧应用程序将继续将其所有警报(包括重复警报)视为精确警报。

val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntentFirst = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntentSecond = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT)
// Set the first alarm to start after 1 minute, repeat the alarm for every 1 minute
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.timeInMillis + 60 * 1000, 60 * 1000, pendingIntentFirst)
// Set the second alarm to start after 2 minutes, repeat the alarm for every 1 minute
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.timeInMillis + 120 * 1000, 60 * 1000, pendingIntentSecond)

通过上述操作,您将观察到两个警报 * 将在2分钟 * 后同时触发。然后它们每隔1分钟重复一次。
对于您的情况,可能是警报同时被触发,因此您可能最终认为AlarmReceiver类只运行一次。
因此,为了有多个警报,您应该使用AlarmManager.setExact()函数:

public void setExact (int type, 
                long triggerAtMillis, 
                PendingIntent operation)

安排在指定时间准确发送警报。

注意:只有强烈要求精确时间发送的闹钟(如闹钟在请求的时间响起)才应安排为精确。强烈建议应用程序不要不必要地使用精确闹钟,因为它们会降低操作系统最大限度减少电池使用的能力。
由于setExact()功能是一次性的,您必须在AlarmReceiver广播类中手动重新安排闹钟。
比如说

val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
// You may find intent.putExtra() useful if you need to pass thing to your AlarmReceiver
val pendingIntentFirst = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntentSecond = PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT)
// Set the first alarm to start after 1 minute
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.timeInMillis + 60 * 1000, pendingIntentFirst)
// Set the second alarm to start after 2 minutes
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.timeInMillis + 120 * 1000,pendingIntentSecond)

在您的AlarmReceiver类中:

class AlarmReceiver : BroadcastReceiver() {
    ...
    override fun onReceive(p0: Context?, p1: Intent?) {
        // Your function here
        // After running your function, you will have to reschedule the alarm here
        val calendar = Calendar.getInstance()
        val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager
        val intent = Intent(this, AlarmReceiver::class.java)
        val pendingIntentFirst = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        // Repeat after 1 minute
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calender.timeInMillis + 60 * 1000, pendingIntentFirst)
    }
}

相关问题