程序死亡时以编程方式处理注销BroadcastReceiver [英] Handle programmatically unregister BroadcastReceiver on process death

查看:84
本文介绍了程序死亡时以编程方式处理注销BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我以编程方式注册BroadcastReceiver,并根据应用的业务逻辑在适当的时间取消注册.

 receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {...}

我想确保当我的应用程序进程意外终止(被杀死)时,接收方是否不会引起内存泄漏?如果这样做,我有什么选择?

(已检查官方文档这篇很棒的文章解决方案

我想确保当我的应用程序进程意外终止(被杀死)时,接收方是否不会引起内存泄漏?如果这样做,我有什么选择?

当某个应用程序进程被系统杀死(完全)时,与该进程相关联的所有东西都消失了".应用组件(ActivityServiceBroadcastReceiver)一侧不会发生内存泄漏.

修改
当进程被杀死时ActivityManagerService使用cleanUpApplicationRecordLocked方法
official doc, this great article and this SO thread )

解决方案

I want to make sure that when my app's process unexpectedly die (get killed) no memory leak is caused by the receiver and if it does what are my options?

When an app process gets killed by the system (quite) everything associated with the process is "gone". No memory leaks from the app component's (Activity, Service, BroadcastReceiver) side can be expected.

Edit
When process get killed ActivityManagerService clean all app resources using cleanUpApplicationRecordLocked method here.
You can clearly see that all resources including all registered broadcast receivers being cleaned.(Ln. 15598)

这篇关于程序死亡时以编程方式处理注销BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆