我正在尝试注册 Fence LocationFence API 以跟踪用户在特定位置的进入、退出或 IN,The Fence 注册成功,但注册的广播接收器未能捕捉到任何LocationFence.in , LocationFence.entering or LocationFence.exiting
我错过了什么吗?(我的位置在印度 +格林威治标准时间 5:30)。请在下面找到我的代码:-
注意:我尝试使用 HeadphoneFence.pluggingIn 和 HeadphoneFence.unplugging 设置栅栏,并且能够在同一个广播接收器中获取这两个事件。
private final String FENCE_LOCATION_RECEIVER_ACTION =
BuildConfig.APPLICATION_ID + "FENCE_LOCATION_RECEIVER_ACTION";
Intent intent1 = new Intent(FENCE_LOCATION_RECEIVER_ACTION);
mLocationPendingIntent=PendingIntent.getBroadcast(MainActivity.this,0,intent1, 0);
FenceLocationReceiver fenceLOcationReceiver = new FenceLOcationReceiver();
registerReceiver(fenceLOcationReceiver, new IntentFilter(FENCE_LOCATION_RECEIVER_ACTION));
checkLocationPermission();
AwarenessFence myFence = AwarenessFence.or(LocationFence.in(28.557659, 77.2459485, 10, 60000L), LocationFence.entering(28.557659, 77.2459485, 10), LocationFence.exiting(28.557659, 77.2459485, 10));
Awareness.FenceApi.updateFences(
mGoogleApiClient,
new FenceUpdateRequest.Builder()
.addFence(LOCATION_FENCE_KEY, myFence, mLocationPendingIntent)
.build())
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(TAG, "here location Fence was successfully registered.");
} else {
Log.e(TAG, "here location Fence could not be registered: " + status);
}
}
});
这是我的广播接收器代码:-
public class FenceLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!TextUtils.equals(FENCE_LOCATION_RECEIVER_ACTION, intent.getAction())) {
txt.setText("Received an unsupported action in FenceReceiver: action="
+ intent.getAction());
return;
}
// The state information for the given fence is em
FenceState fenceState = FenceState.extract(intent);
if (TextUtils.equals(fenceState.getFenceKey(), FENCE_LOCATION_RECEIVER_ACTION)) {
String fenceStateStr;
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
fenceStateStr = "true";
txt.setText(fenceStateStr);
break;
case FenceState.FALSE:
fenceStateStr = "false";
txt.setText(fenceStateStr);
break;
case FenceState.UNKNOWN:
fenceStateStr = "unknown";
txt.setText(fenceStateStr);
break;
default:
fenceStateStr = "unknown value";
txt.setText(fenceStateStr);
}
txt.setText("Fence state:" + fenceStateStr);
}
}
}
这里清单中定义的权限:-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />