当我尝试在三星设备中跟踪开始步行或停止步行时,意识无法识别并且不会更新当前状态。
的活动:
公共类MainActivity扩展AppCompatActivity实现了GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "awareness_poc";
private GoogleApiClient googleApiClient;
private PendingIntent requestPendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(FenceReceiver.FENCE_RECEIVER_ACTION);
requestPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
}
@Override
protected void onStart() {
super.onStart();
googleApiClient = new GoogleApiClient.Builder(this, this, this)
.addApi(Awareness.API)
.build();
googleApiClient.connect();
}
public void removeFences(View view) {
FenceUpdateRequest request = new FenceUpdateRequest.Builder()
.removeFence(WALKING_START_KEY)
.removeFence(WALKING_STOP_KEY)
.build();
Awareness.FenceApi.updateFences(googleApiClient, request)
.setResultCallback(new ResultCallbacks<Status>() {
@Override
public void onSuccess(@NonNull Status status) {
Log.i(TAG, "Fences successfully removed.");
}
@Override
public void onFailure(@NonNull Status status) {
Log.i(TAG, "Fences could NOT be removed.");
}
});
}
@Override
public void onConnected(@Nullable Bundle bundle) {
AwarenessFence walkingStartFence = DetectedActivityFence.starting(DetectedActivityFence.WALKING);
AwarenessFence walkingStopFence = DetectedActivityFence.stopping(DetectedActivityFence.WALKING);
FenceUpdateRequest request = new FenceUpdateRequest.Builder()
.addFence(WALKING_START_KEY, walkingStartFence, requestPendingIntent)
.addFence(WALKING_STOP_KEY, walkingStopFence, requestPendingIntent)
.build();
Awareness.FenceApi.updateFences(googleApiClient, request)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(TAG, "Fence was successfully registered.");
} else {
Log.e(TAG, "Fence could not be registered: " + status);
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
广播接收器
ublic class FenceReceiver extends BroadcastReceiver {
public static final String FENCE_RECEIVER_ACTION =
"com.iwsbrazil.lab.awarenesspoc.FENCE_RECEIVER_ACTION";
public static final String WALKING_START_KEY = "walking_start";
public static final String WALKING_STOP_KEY = "walking_stop";
@Override
public void onReceive(Context context, Intent intent) {
FenceState fenceState = FenceState.extract(intent);
if (fenceState.getCurrentState() == FenceState.TRUE) {
if (TextUtils.equals(fenceState.getFenceKey(), WALKING_START_KEY)) {
sendNotification(context, "You Started Walking", 1);
} else if (TextUtils.equals(fenceState.getFenceKey(), WALKING_STOP_KEY)) {
sendNotification(context, "You Stopped Walking", 2);
}
}
}
private void sendNotification(Context context, String text, int id) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Awareness")
.setContentText(text)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(id, builder.build());
}
}