1

I try to integrate the beaconinside sdk into my app and I want to receive the events sent through the Beaconservice even when my app is closed and removed from the app drawer (recent apps).

Now my code looks like so

BeaconFragment

@Override
public void onCreate() {
    super.onCreate();

    // init beaconinside sdk using my token
    BeaconService.init(this, API_TOKEN);
}

BeaconHandler

public class BeaconHandler extends BroadcastReceiver {
    // ...

    public void onReceive(Context context, Intent intent) {
        // ...
    }
}

BackgroundBeaconService

public class BackgroundBeaconservice extends IntentService {
    // ...
    public void onCreate() {
        // setup intent filter for beacon events
        IntentFilter beaconListenerFilter = new IntentFilter();
            // add intent filter for BeaconService (e.g. REGION_ENTER)

        // beacon event handler
        beaconListener = new BeaconHandler(this);

        // register the event handler to the LocalBroadcastManager
        registerBeaconEventHandler(beaconListener, beaconListenerFilter);
    }

    private void registerBeaconEventHandler(BroadcastReceiver receiver, IntentFilter filter) {
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter);
    }
}

So far it works but when I close the app from the recent app drawer, it stops working. I wrote to the Tech Support of Beaconinside and they told me to use a Service to receive events from the LocalBroadcastManager in the background.

Which aspects of my code do I need to move in the service class and which can stay where they are?

LimitX
  • 595
  • 3
  • 7
  • 23
  • That's right that you need a service.Service should register a receiver, and handle events from the receiver. – Vladyslav Matviienko Apr 28 '17 at 07:54
  • So the IntentService will then receive the intent sent from the BeaconService, because it sends a broadcast using the LocalBroadcastManager, right? – LimitX Apr 28 '17 at 13:02
  • if you set it up correctly, then yes – Vladyslav Matviienko Apr 28 '17 at 14:33
  • I now moved all my Code in an IntentService, so in the **onCreate** method I registered my BroadcastReceiver. It works, but again if I kill the app, my BroadcastReceiver doesn't receive events anymore. – LimitX May 02 '17 at 09:29
  • yes, that's right, the only service which is supposed to work even after app is killed is a foreground service. – Vladyslav Matviienko May 02 '17 at 19:11
  • So it's not possible in android to run a service in background when the app is closed, **without the user noticing it**? A foreground service [must provide a notification for the status bar](https://developer.android.com/guide/components/services.html#Foreground), without that it is not possible. – LimitX May 03 '17 at 13:07
  • 1
    yes, exactly. That was done for user to know that something is done in background. – Vladyslav Matviienko May 03 '17 at 13:21

1 Answers1

0

As it turns out, this problem can't be solved without the interaction of beaconinside. So I sent a mail to them and they told me that this problem is fixed in the new version (2.8.0), which will be released this week.

LimitX
  • 595
  • 3
  • 7
  • 23