Search code examples
ibeacon-androidandroid-ibeaconaltbeacon

POJO as a BeaconConsumer instead of Activity


I am trying to use a POJO as a BeaconConsumer. Is this acceptable? The onServiceConnect is getting called. But I have to forcefully override the unbindService and the bindService.


Solution

  • In general, the BeaconConsumer interface is designed to work with an Activity, Service or Application instance. There's nothing wrong using a POJO as a BeaconConsumer, but it must hold a reference to the Context. The POJO's bindService and unbindService methods must be chained to the equivalent methods on the Context like this:

    @Override
    public boolean bindService(Intent intent, ServiceConnection conn, int mode) {
        return context.bindService(intent, conn, mode);
    }
    
    @Override
    public void unbindService(ServiceConnection conn) {
        context.unbindService(conn);
    }
    

    Also, take care that something long-living in the Android lifecycle (an Activity, Application, etc.) holds a reference to the POJO so it doesn't get garbage collected of during the Android lifecycle.