简体   繁体   English

从 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?

I am currently developing an Android app using Android Studio.我目前正在使用 Android Studio 开发 Android 应用程序。 One of my requirements is to trigger the BroadcastReceiver at an exact time.我的要求之一是在准确的时间触发BroadcastReceiver It is working perfectly fine until the app is running in the background.在应用程序在后台运行之前,它工作得非常好。 Once the app is killed from the recent apps list, it doesn't work.一旦应用程序从最近的应用程序列表中被杀死,它就不起作用了。 I don't know whether the problem is in the alarm manager or in the BroadcastReceiver .我不知道问题出在警报管理器中还是在BroadcastReceiver中。

XML: XML:

<receiver android:name=".BackgroundServer"
    android:permission="TODO"
    tools:ignore="ExportedReceiver">
    <intent-filter>
        <action android:name="birthday.wallpaper.WALLPAPER_CHANGE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

And in MainActivity (Java):在 MainActivity (Java) 中:

Intent intent = new Intent(this,BackgroundServer.class);

PendingIntent pendingIntent = PendingIntent
        .getBroadcast(getApplicationContext(), 0,intent,0);

AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert manager != null;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY,
        pendingIntent
);

In my custom Broadcast Receiver, I have set the alarm manager in the onStart method of MainActivity .在我的自定义广播接收器中,我在MainActivityonStart方法中设置了警报管理器。 This means, like alarms, the BroadcastReceiver 's code must be executed.这意味着,像警报一样,必须执行BroadcastReceiver的代码。

Well, it works when the app is in the foreground or when the app is closed but present in the recent apps list.好吧,当应用程序位于前台或应用程序关闭但出现在最近的应用程序列表中时,它可以工作。 The code is not executed when I try to remove the app from the recent apps list.当我尝试从最近的应用程序列表中删除该应用程序时,该代码未执行。 I don't know whether the alarm is disabled when I remove the app from the recent apps list, or perhaps the Broadcast Receiver stops listening to Broadcasts.我不知道当我从最近的应用程序列表中删除应用程序时警报是否被禁用,或者广播接收器可能停止收听广播。

You used a dynamic declared receiver.您使用了动态声明的接收器。 This means that it is created and registered in your activity.这意味着它是在您的活动中创建和注册的。 You declare this type of receivers and register in onResume().When you kill your activity you have to unregister it in onStop().您声明这种类型的接收器并在 onResume() 中注册。当您终止您的活动时,您必须在 onStop() 中取消注册。 If you don't unregister, you are going to get an error called LeakIntentMemory, however activity unregister it by itself and app don't crashes.如果您不取消注册,您将收到一个名为 LeakIntentMemory 的错误,但是活动会自行取消注册,并且应用程序不会崩溃。

But when you look at logcat you are going to see the LeakIntentMemory error.但是当您查看 logcat 时,您将看到 LeakIntentMemory 错误。 And this type of receivers works as you tell.When you close app you can not receive anything.这种类型的接收器如您所说的那样工作。当您关闭应用程序时,您将收不到任何东西。 If you want to receive something whenever your app closed, you have to declare your receiver in android manifest.如果你想在你的应用关闭时收到一些东西,你必须在 android 清单中声明你的接收者。 For more examples check my GitHub https://github.com/okanSerdaroglu/BroadcastReceivers有关更多示例,请查看我的 GitHub https://github.com/okanSerdaroglu/BroadcastReceivers

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM