Google Awareness Api events while the application is not running

I would like either BroadcastReceiver or IntentService (depending on how long my processing takes) to start when the Awareness API fires. For example, perhaps I want to know how many times I activate a set of beacon shutters during the day (provided that I keep my phone with me). All the examples I found show the registration of broadcast receivers in the code, but I understand that I will need to register the broadcast receiver in the manifest so that the OS can send it a broadcast if my application does not work. What else, the intent identifier looks like a custom identifier, so I would suggest that I have to register it with the OS at least once with code?

I suppose that I will need to create one or more test applications to understand this by trial and error, but I would be glad to hear from everyone who tried this and would like to share your results!

+6
source share
1 answer

This is sufficient if you specify BroadCastReceiver in the manifest file.

It is not necessary that you need to register it in the code even after declaring the Manifest <receiver> record. Just think about how the platform can handle operations that you register only in the manifest file (if not we get an ActivityNotFoundException) in the same way. Broadcasts can also be recorded only in the manifest file.

You need to declare the receiver as:

 <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 information at https://developer.android.com/guide/topics/manifest/receiver-element.html

+5
source

All Articles