如果 activity 进入 onStop,注册到 activity 上下文的广播接收器是否仍然响应?

Do broadcast receivers registered with an activity's context still respond if the activity goes into onStop?

如果一个activity注册了一个广播接收器,如果它已经被置于后台(onStop()),它是否仍然能够响应通知?

是的,注册的广播接收器将始终能够接收广播,直到您取消注册,或者应用程序进程被系统杀死以回收内存。

所以当您不再需要它时请记得取消注册,否则您可能会留下潜在的内存泄漏。

建议:

Be mindful of where you register and unregister the receiver, for example, if you register a receiver in onCreate(Bundle) using the activity's context, you should unregister it in onDestroy() to prevent leaking the receiver out of the activity context. If you register a receiver in onResume(), you should unregister it in onPause() to prevent registering it multiple times (If you don't want to receive broadcasts when paused, and this can cut down on unnecessary system overhead).

Here 进一步阅读

https://developer.android.com/guide/components/broadcasts#context-registered-receivers所述:

Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.

所以我认为 activity 生命周期的 onDestroy 方法应该是您关注的问题。