简体   繁体   中英

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:

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

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.

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. There is no chance for leak in case if it is binded with Service life cycle.

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