简体   繁体   中英

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. One of my requirements is to trigger the BroadcastReceiver at an exact time. 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 .

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):

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 . This means, like alarms, the BroadcastReceiver 's code must be executed.

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(). 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.

But when you look at logcat you are going to see the LeakIntentMemory error. 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. For more examples check my GitHub https://github.com/okanSerdaroglu/BroadcastReceivers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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