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
source share