在华为和三星手机上关闭应用程序时,警报经理无法正常工作

发布于 2025-02-05 14:06:48 字数 2339 浏览 3 评论 0原文


I created a Kotlin app who should give an alarm when a defined number of thinks is reached. In the emulator it is working fine and also when the app is open. But when the app is closed, the alarm is not triggered on my Huawei and Samsung phone. I tried the following:
  • 更改手机上的设置(应用通知,电池管理)
  • 在不同的电话(华为和三星)上尝试一下,
  • 中添加了“警报”单词,
  • 我在构建和尝试的发布版本

不幸的是,没有任何帮助。

private fun setAlarm(timeInMillis: Long) {
    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, MyAlarmReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
    alarmManager.set(
        AlarmManager.RTC,
        timeInMillis,
        pendingIntent
    )
    //Alarm deaktiviert
    if ((actual_alarm_on_off =="OFF" && actual_alarm_on_off != "ON1") || (alarmReleased == true && actual_alarm_on_off != "ON1")){
        alarmManager.cancel(pendingIntent)
    }
}

class MyAlarmReceiver: BroadcastReceiver(){

companion object {
    const val NOTIFICATION_ID = 100
    const val NOTIFICATION_CHANNEL_ID = "1000"
}

override fun onReceive(context: Context, intent: Intent) {
    createNotificationChannel(context)
    notifyNotification(context)
}

private fun createNotificationChannel(context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            "@string/app",
            NotificationManager.IMPORTANCE_HIGH
        )
        NotificationManagerCompat.from(context).createNotificationChannel(notificationChannel)
    }
}

private fun notifyNotification(context: Context) {
    with(NotificationManagerCompat.from(context)) {
        val build = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("Pillen-Zähler Alarm")
            .setContentText("Medikament bald aufgebraucht!")
            .setSmallIcon(R.drawable.ic_baseline_warning_24)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
        notify(NOTIFICATION_ID, build.build())
    }
}

}


<receiver android:name="com.example.medbesteller_alarm.MyAlarmReceiver"
        android:exported="false"/>

I created a Kotlin app who should give an alarm when a defined number of thinks is reached. In the emulator it is working fine and also when the app is open. But when the app is closed, the alarm is not triggered on my Huawei and Samsung phone.
I tried the following:

  • Change settings on Phones (app notifications, battery management)
  • Try it on different phones (Huawei and Samsung)
  • I added the words "alarm" to the package name
  • Built and tried release version

Unfortunately nothing helps.

private fun setAlarm(timeInMillis: Long) {
    val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, MyAlarmReceiver::class.java)
    val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)
    alarmManager.set(
        AlarmManager.RTC,
        timeInMillis,
        pendingIntent
    )
    //Alarm deaktiviert
    if ((actual_alarm_on_off =="OFF" && actual_alarm_on_off != "ON1") || (alarmReleased == true && actual_alarm_on_off != "ON1")){
        alarmManager.cancel(pendingIntent)
    }
}

class MyAlarmReceiver: BroadcastReceiver(){

companion object {
    const val NOTIFICATION_ID = 100
    const val NOTIFICATION_CHANNEL_ID = "1000"
}

override fun onReceive(context: Context, intent: Intent) {
    createNotificationChannel(context)
    notifyNotification(context)
}

private fun createNotificationChannel(context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(
            NOTIFICATION_CHANNEL_ID,
            "@string/app",
            NotificationManager.IMPORTANCE_HIGH
        )
        NotificationManagerCompat.from(context).createNotificationChannel(notificationChannel)
    }
}

private fun notifyNotification(context: Context) {
    with(NotificationManagerCompat.from(context)) {
        val build = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
            .setContentTitle("Pillen-Zähler Alarm")
            .setContentText("Medikament bald aufgebraucht!")
            .setSmallIcon(R.drawable.ic_baseline_warning_24)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
        notify(NOTIFICATION_ID, build.build())
    }
}

}

<receiver android:name="com.example.medbesteller_alarm.MyAlarmReceiver"
        android:exported="false"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文