简体   繁体   English

Android 警报管理器不工作,而应用程序被最近的应用程序杀死

[英]Android Alarm manager not working while appliction is killed from recent apps

I am stuck at Reminders in my application.我被困在我的应用程序的提醒中。 The Reminders with Alarm Manager is working fine when my app is running or is in background (Recent Apps) but the main issue is that when i kill my app the alarm does not trigger.当我的应用程序正在运行或处于后台(最近的应用程序)时,带有警报管理器的提醒工作正常,但主要问题是当我杀死我的应用程序时警报不会触发。 Should i implement any background Service for keeping my alarm alive even when the application is killed?即使应用程序被杀死,我是否应该实施任何后台服务来保持我的警报活跃? My Menifest Code:我的清单代码:

<receiver android:name=".ui.home.reminders.AlarmReceiver"
        android:process=":remote"
        android:exported="true"
        android:enabled="true"/>

My Receiver Class:我的接收器 Class:

class AlarmReceiver: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
    p0?.showToast("received")
}

} }

My Fragment for setting Alarm:我设置闹钟的片段:

private val calendar: Calendar by lazy {
    Calendar.getInstance()
}

I am setting Time through TimePickerDialog:我正在通过 TimePickerDialog 设置时间:

val listener = TimePickerDialog.OnTimeSetListener { p0, p1, p2 ->
                    activity?.showToast(" $p1 $p2")
                    calendar.set(Calendar.HOUR_OF_DAY, p1)
                    calendar.set(Calendar.MINUTE, p2)
                    calendar.set(Calendar.SECOND, 0)
                }
                TimePickerDialog(
                    activity!!,
                    listener,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    false
                ).show()

On click of set Alarm Button:单击设置警报按钮时:

val alarmManager =
                    Constants.context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
                val pendingIntent = PendingIntent.getBroadcast(
                    Constants.context,
                    0,
                    Intent(
                        Constants.context,
                        AlarmReceiver::class.java
                    ),
                    0
                )
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)

I have seen various examples but none of them seems to be working for me.我见过各种各样的例子,但似乎没有一个对我有用。 If i have to implement a background service or there is another way of implementing Reminder with notification then please suggest some example code snippet.如果我必须实现后台服务或者有另一种实现提醒的方法,那么请建议一些示例代码片段。 Scenario: I want to show reminder with a notification ( Time / Date / Repetition- Daily/Monthly will also be decided by the user).场景:我想通过通知显示提醒(时间/日期/重复-每日/每月也将由用户决定)。 Any help will be appericiated.任何帮助都会得到帮助。 Thanks in advance.提前致谢。

You created the intent, but the notification is displayed outside your app.您创建了意图,但通知显示在您的应用程序之外。 To make an intent work outside your app, you need to create a new PendingIntent.要使 Intent 在您的应用之外工作,您需要创建一个新的 PendingIntent。

PendingIntent grants rights to another application or the system to perform an operation on behalf of your application. PendingIntent 授予另一个应用程序或系统代表您的应用程序执行操作的权利。 A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. PendingIntent 本身只是对系统维护的令牌的引用,该令牌描述了用于检索它的原始数据。 This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes it has been given to.这意味着,即使它拥有的应用程序的进程被杀死,PendingIntent 本身仍然可以从它被给予的其他进程中使用。 In this case, the system will use the pending intent to open the app on behalf of you, regardless of whether or not the timer app is running.在这种情况下,无论计时器应用程序是否正在运行,系统都会使用挂起的意图代表您打开应用程序。

I suggest Walking Through below CodeLab.我建议在 CodeLab 下方穿行。 It's So handy.它太方便了。

https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0 https://developer.android.com/codelabs/advanced-android-kotlin-training-notifications#0

I Hope It'll Help.我希望它会有所帮助。

Okay, after so much struggling with the above issue I myself found the solution, I tried launching the background Service for the same but the system was shutting it down too after i was removing the app from the recent apps, THE MAIN CULPRIT WAS BATTERY OPTIMIZATION SETTINGS .好的,在为上述问题苦苦挣扎之后,我自己找到了解决方案,我尝试启动后台服务,但在我从最近的应用程序中删除应用程序后,系统也将其关闭,主要罪魁祸首是电池优化设置 After i removed the battery optimization setting from the settings, the alarm just worked fine as i wanted:) I am posting this answer so that it can help others and save their time... happy coding...在我从设置中删除电池优化设置后,警报就像我想要的那样正常工作:) 我发布了这个答案,以便它可以帮助其他人并节省他们的时间......快乐编码......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序被杀死时,Android 警报管理器不起作用 - Android Alarm Manager Not working when app is Killed 从 Android 中的最近应用程序列表中删除应用程序是否会导致它禁用警报管理器在 Android 应用程序中设置的任何警报? - Does removing an app from the recent apps list in Android cause it to disable any alarms that have been set by the alarm manager in an Android app? 只希望将警报从“最新应用”中删除或用户被强制杀死时死亡 - Only want Alarm to die when removed from Recent Apps or user forced killed Android:如何检测从最近的应用列表中杀死的应用? - Android : How to detect app killed from recent apps list? 警报管理器Android无法正常工作 - alarm manager android not working Android警报管理器不起作用 - Android alarm manager not working android:-Alarm管理器不工作 - android :-Alarm manager not working 我的应用在Android中被杀死后,我的警报管理器被杀死 - My alarm manager killed after my app killed in android 在一加3t移动设备上杀死应用程序或从最近使用的应用程序中刷出应用程序时,Android FCM推送通知不起作用 - Android FCM Push Notification Not Working When App is being Killed or Swipe Out from Recent Apps on One plus 3t Mobile 当用户从最近的应用程序列表中删除应用程序时,如何取消警报管理器警报? - How to cancel alarm manager alarms when user removes the app from recent apps list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM