英文:
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?
问题
我目前正在使用Android Studio开发Android应用程序。我的一个要求是在确切的时间触发BroadcastReceiver
。在应用程序在后台运行时,它运行得非常完美。但是一旦我从最近使用的应用程序列表中将应用程序关闭,它就不起作用了。我不知道问题是在闹钟管理器还是在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>
并且在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
);
在我的自定义广播接收器中,我在MainActivity
的onStart
方法中设置了闹钟管理器。这意味着,与闹钟一样,BroadcastReceiver
的代码必须被执行。
嗯,在应用程序在前台运行或者应用程序被关闭但仍在最近使用的应用程序列表中时,它都能正常工作。但是当我尝试从最近使用的应用程序列表中移除应用程序时,代码就不会被执行。我不知道当我将应用程序从最近使用的应用程序列表中移除时,闹钟是否被禁用,或者是广播接收器停止监听广播了。
英文:
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.
答案1
得分: 0
你使用了动态声明的接收器。这意味着它在你的活动中被创建并注册。你声明了这类型的接收器并在onResume()中注册。当你关闭活动时,你必须在onStop()中取消注册它。如果你不取消注册,就会出现一个叫做LeakIntentMemory的错误,然而活动会自行取消注册,应用程序不会崩溃。
但是当你查看logcat时,你会看到LeakIntentMemory错误。这类型的接收器按照你所说的方式工作。当你关闭应用程序时,就无法接收任何内容。如果你想在应用程序关闭时仍然接收内容,你必须在Android清单文件中声明你的接收器。更多示例请查看我的GitHub,网址是https://github.com/okanSerdaroglu/BroadcastReceivers。
英文:
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
专注分享java语言的经验与见解,让所有开发者获益!
评论