Google Awareness Api events while app isn't running

自古美人都是妖i 提交于 2019-12-05 07:48:23
Kowshick

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!