Ziv Kesten Ziv Kesten - 1 month ago 22
Android Question

Awareness API global receiver intent filter

i am implementing the google Awareness API in an android app, but none of the samples, nor the guides, shows how to listen to the api events while the app is down or in the background.
I wrote a global receiver based on the answer here

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


However, since it is not working, i suspect i don't know the correct intent filter for intercepting the Awarness events.
Does anyone know the correct intent filter, or if this is not my issue, how can i intercept the API events while the app is down or in the background with a global receiver?

Answer

Ok so the answer eventually was that you have to register this receiver in code and give it your own intent filter like this:

   Intent intent = new Intent(Constants.Requests.FENCE_RECEIVER_ACTION);
                    mPendingIntent =
                            PendingIntent.getBroadcast(BaseActivity.this, 0, intent, 0);

                    // The broadcast receiver that will receive intents when a fence is triggered.
                    registerReceiver(new FenceReceiver(), new IntentFilter(FENCE_RECEIVER_ACTION));

where "FENCE_RECEIVER_ACTION" is this:

// The intent action which will be fired when your fence is triggered.
    public final static String FENCE_RECEIVER_ACTION =
            BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION";

And in the manifest you put in the receiver like this:

 <receiver android:name=".FenceReceiver" >
        <intent-filter>
            <action android:name="'YOUR_PACKGE_NAME_HERE'FENCE_RECEIVER_ACTION" />
        </intent-filter>
    </receiver>

There is no need to unregister the receiver anywhere.