简体   繁体   English

广播接收器和内存泄漏

[英]Broadcast receiver and memory leaks

In my app; 在我的应用中; I want to receive network connectivity changes in all my services since I have to manage db locally as well as on server both. 我想在所有服务中接收网络连接更改,因为我必须同时在本地和服务器上管理数据库。

I have made BaseConnectivityService which has receiver for connectivity change as follows: 我已经制作了BaseConnectivityService ,它具有如下接收器,用于进行连接更改:

public abstract class BaseConnectivityService extends IntentService {

    private ConnectivityManager mConnectivityManager;

    public BaseConnectivityService(String name) {
        super(name);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mConnectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        registerReceivers();
    }

    @Override
    public void onDestroy() {
        unregisterReceivers();
        mConnectivityManager = null;
        super.onDestroy();
    }

    private void registerReceivers() {
        IntentFilter connectivityIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mConnectivityChangeReceiver, connectivityIntentFilter);
    }

    private void unregisterReceivers() {
        unregisterReceiver(mConnectivityChangeReceiver);
    }

    /**
     * will be invoked when network connectivity has been changed and network is in connected state
     */
    protected abstract void onNetworkConnected();

    /**
     * will be invoked when network connectivity has been changed and network is NOT in connected state
     */
    protected abstract void onNetworkDisconnected();

    /**
     * checks whether network is available and is in connected state
     *
     * @return true if network is connected; false otherwise
     */
    protected final boolean isNetworkConnected() {
        NetworkInfo activeNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected();
    }


    private final BroadcastReceiver mConnectivityChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (isNetworkConnected()) {
                onNetworkConnected();
            } else {
                onNetworkDisconnected();
            }
        }
    };
}

Now if I extend all my services with BaseConnectivityService ; 现在,如果我使用BaseConnectivityService扩展所有服务; will it cause memory leak as that system will keep the services in memory for the purpose of notifying connectivity changes? 它会导致内存泄漏,因为该系统会将服务保留在内存中以通知连接更改吗?

I have read this thread ; 我已经阅读了这个主题 ; which is somewhat similar question. 这有点类似的问题。 As Mark Murphy has written there : 正如马克·墨菲(Mark Murphy)在那写的那样:

You will leak memory, as your BroadcastReceiver will keep the component in RAM (even if it was destroyed) until such time as Android terminates the process. 您将泄漏内存,因为BroadcastReceiver会将组件保留在RAM中(即使它已销毁),直到Android终止进程为止。

Is it be applicable for my scenario? 它适用于我的情况吗? If yes, what can be the good solution for avoiding memory leak and getting notified of network connectivity changes? 如果是,那么什么是避免内存泄漏并获得网络连接更改通知的好的解决方案?

Look at the last response in the same thread. 查看同一线程中的最后一个响应。 You can register and unregister broadcast receiver from Service class. 您可以从Service类注册和注销广播接收器。 There is no chance for leak in case if it is binded with Service life cycle. 如果它与服务生命周期绑定在一起,则没有泄漏的机会。

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

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