I recently started playing with the Google Play Fence API and I see in it a lot of potential for optimizing my application.
The thing I want to achieve is that I want to use the user's current activity โspeedโ to determine the frequency with which I execute some kind of battery.
The barrage API seemed very interesting, as it would have a nice advantage to give me a signal when user activity transitions occur, without having to constantly check.
The problem I am facing is that with my configuration I receive signals once or twice, but never again . I well know that this is the best estimate of effort, but I tried to have a testrun for several hours, but the fences just worked the first couple of times. I believe, maybe I'm misinterpreting the documentation for the Fence API.
The beep is muted, and I work as planned. These are just activity transition signals that behave strangely.
Here is my configuration
Some constants:
public static final String ACTION_AWARENESS_FENCE =
BuildConfig.APPLICATION_ID + ".action.AWARENESS_CHANGED";
public static final String HEADPHONES = "headphones";
public static final String IN_VEHICLE = "inVehicle";
public static final String ON_BIKE = "onBike";
public static final String ON_FOOT = "onFoot";
public static final String STILL = "still";
Fencing Registration
AwarenessFence onFootFence = DetectedActivityFence.during(DetectedActivityFence.ON_FOOT);
AwarenessFence inVehicleFence = DetectedActivityFence.during(DetectedActivityFence.IN_VEHICLE);
AwarenessFence stillFence = DetectedActivityFence.during(DetectedActivityFence.STILL, DetectedActivityFence.TILTING, DetectedActivityFence.UNKNOWN);
AwarenessFence onBikeFence = DetectedActivityFence.during(DetectedActivityFence.ON_BICYCLE);
AwarenessFence debugFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);
Awareness.FenceApi.updateFences(client, new FenceUpdateRequest.Builder().addFence(ON_FOOT, onFootFence, createAwarenessIntent(ON_FOOT))
.addFence(IN_VEHICLE, inVehicleFence, createAwarenessIntent(IN_VEHICLE))
.addFence(STILL, stillFence, createAwarenessIntent(STILL))
.addFence(ON_BIKE, onBikeFence, createAwarenessIntent(ON_BIKE))
.addFence(HEADPHONES, debugFence, createDebugAwarenessIntent())
.build())
.setResultCallback(status -> {
});
private PendingIntent createAwarenessIntent(String key) {
Intent trackIntent = createAwareness(mContext, key);
return PendingIntent.getBroadcast(mContext, key.hashCode(), trackIntent, PendingIntent.FLAG_CANCEL_CURRENT);
}
public static Intent createAwareness(Context context, String key) {
Intent trackIntent = new Intent(context, GoogleAwarnessReceiver.class);
trackIntent.setAction(ACTION_AWARENESS_FENCE + "." + key);
return trackIntent;
}
The host:
public class GoogleAwarnessReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (StringUtil.contains(intent.getAction(), ACTION_AWARENESS_FENCE)) {
}
}
}
<receiver
android:name=".GoogleAwarnessReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="${applicationId}.action.AWARENESS_CHANGED.inVehicle"/>
<action android:name="${applicationId}.action.AWARENESS_CHANGED.still"/>
<action android:name="${applicationId}.action.AWARENESS_CHANGED.onFoot"/>
<action android:name="${applicationId}.action.AWARENESS_CHANGED.headphones"/>
<action android:name="${applicationId}.action.AWARENESS_CHANGED.onBike"/>
</intent-filter>
</receiver>
EDIT: Linking: DetectedActivityFence not starting? which seems to be a problem very similar to the one I am having.