6

I would like either a BroadcastReceiver or IntentService (depending on how long my eventual processing takes) to start when a Google Awareness API "fence" fires. For example, perhaps I want to know how many times I activate a set of beacon fences over the course of the day (assuming I keep my phone with me). All the examples I've found show registering broadcast receivers in code, but my understanding is that I would need to register a broadcast receiver in the manifest in order for the OS to send the broadcast to it if my app isn't running. What's more, the intent ID appears to be a custom one, so I would guess I'd have to register it with the OS at least once via code?

I'm guessing I'm going to have to create one or more test apps to figure this out by trial and error, but would sure appreciate hearing from anyone who has tried this and would like to share your results!

William T. Mallard
  • 1,562
  • 2
  • 25
  • 33

1 Answers1

5

It is just enough if you specify BroadCastReceiver in your Manifest file.

Its not a must that you need to register it in the code even after declaring the Manifest <receiver> entry. Just think about how the platform is able to handle Activities you register it only in the Manifest file(if not we get ActivityNotFoundException) the same way Broadcasts can also be register only in the Manifest file.

You need to declare the receiver like:

<receiver android:name=".MyFenceReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.FENCE_RECEIVER_ACTION" />
    </intent-filter>
</receiver>

Extend the BroadcastReceiver class.

public class MyFenceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    FenceState fenceState = FenceState.extract(intent);

    if (TextUtils.equals(fenceState.getFenceKey(), "geofence")) {
        switch(fenceState.getCurrentState()) {
            case FenceState.TRUE:

                break;
            case FenceState.FALSE:

                break;
            case FenceState.UNKNOWN:

                break;
        }
    }
}
}

More info in https://developer.android.com/guide/topics/manifest/receiver-element.html

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Kowshick
  • 142
  • 3
  • Right, I got that far. Awarding the bounty for the effort! I'm still stuck at getting the fence registered in a persistent way. I haven't had a lot of time for it lately but my last testing showed that the receiver would get called for only a few times before it would stop. All the receiver was doing was popping up a toast. I'll try with a log file next. – William T. Mallard Oct 06 '16 at 18:27
  • Is this the correct intent filter to intercept awarness events? i am not getting the events when the app is down... – Ziv Kesten Oct 18 '16 at 14:27
  • @ZivKesten You are not receiving the event when Application is not running??? That means you are receiving them when your application is running right. So it has to do with how you have registered for Broadcast. I am guessing you have not registered it in the Manifest file but in the code and you are using Activity context instead of Application context. Some code sample would help to understand your scenario better. – Kowshick Oct 19 '16 at 21:57
  • I did write it in the manifest, I don't think global receivers needs to be written anywhere else, which is why I think I may have misunderstood the intent filter. – Ziv Kesten Oct 20 '16 at 03:29
  • I have placed the reciver in the manifest exactly as you did: and did not register it in the code – Ziv Kesten Oct 20 '16 at 06:08
  • we can use GCM/ FCM Job scheduler or custom instead Job scheduler lib instead of BroadcastReceiver? – LOG_TAG Aug 03 '17 at 12:48