我嘗試使用Google Awareness API實施活動柵欄。但用戶活動的變化未被檢測到。耳機圍欄按預期工作。活動柵欄在Awareness中不起作用API
ActivityFenceActivity
public class ActivityFenceActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {
private static final String FENCE_RECEIVER_ACTION = "FENCE_RECEIVE";
private static final String FENCE_WALKING_KEY = "walkingKey";
private static final String FENCE_RUNNING_KEY = "runningKey";
private static final String TAG = ActivityFenceActivity.class.getSimpleName();
private GoogleApiClient googleApiClient;
private TextView activityTextView;
private BroadcastReceiver activityFenceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context, "Recieved", Toast.LENGTH_SHORT).show();
FenceState fenceState = FenceState.extract(intent);
if (TextUtils.equals(fenceState.getFenceKey(), FENCE_WALKING_KEY)) {
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
activityTextView.setText("User is walking");
break;
case FenceState.FALSE:
activityTextView.setText("User is not walking");
break;
case FenceState.UNKNOWN:
activityTextView.setText("Activity state unknown");
break;
}
} else if (TextUtils.equals(fenceState.getFenceKey(), FENCE_RUNNING_KEY)) {
switch (fenceState.getCurrentState()) {
case FenceState.TRUE:
activityTextView.setText("User is running");
break;
case FenceState.FALSE:
activityTextView.setText("User is not running");
break;
case FenceState.UNKNOWN:
activityTextView.setText("Activity state unknown");
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_fence);
activityTextView = (TextView) findViewById(R.id.activityTextView);
googleApiClient = new GoogleApiClient.Builder(ActivityFenceActivity.this)
.addApi(Awareness.API)
.addConnectionCallbacks(this)
.build();
googleApiClient.connect();
findViewById(R.id.register_fence).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerActivityFence();
}
});
findViewById(R.id.unregister_fence).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unregisterActivityFence();
}
});
}
@Override
public void onConnected(@Nullable final Bundle bundle) {
Log.d(TAG, "Google API connected");
}
@Override
public void onConnectionSuspended(final int i) {
Log.d(TAG, "Google API connection suspended");
}
@Override
protected void onStart() {
super.onStart();
registerReceiver(activityFenceReceiver, new IntentFilter(FENCE_RECEIVER_ACTION));
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(activityFenceReceiver);
unregisterActivityFence();
}
private void registerActivityFence() {
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
AwarenessFence runningFence = DetectedActivityFence.during(DetectedActivityFence.RUNNING);
PendingIntent fencePendingIntent = PendingIntent.getBroadcast(this,
0,
new Intent(FENCE_RECEIVER_ACTION),
0);
Awareness.FenceApi.updateFences(googleApiClient, new FenceUpdateRequest.Builder()
.addFence(FENCE_WALKING_KEY, walkingFence, fencePendingIntent).build())
.setResultCallback(new ResultCallbacks<Status>() {
@Override
public void onSuccess(@NonNull final Status status) {
Toast.makeText(ActivityFenceActivity.this,
"Fence registered successfully",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(@NonNull final Status status) {
Toast.makeText(ActivityFenceActivity.this,
"Cannot register activity fence.",
Toast.LENGTH_SHORT).show();
}
});
Awareness.FenceApi.updateFences(googleApiClient, new FenceUpdateRequest.Builder()
.addFence(FENCE_RUNNING_KEY, runningFence, fencePendingIntent).build());
}
private void unregisterActivityFence() {
Awareness.FenceApi.updateFences(
googleApiClient,
new FenceUpdateRequest.Builder()
.removeFence(FENCE_WALKING_KEY)
.removeFence(FENCE_RUNNING_KEY)
.build()).setResultCallback(new ResultCallbacks<Status>() {
@Override
public void onSuccess(@NonNull Status status) {
Toast.makeText(ActivityFenceActivity.this,
"Fence unregistered successfully.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(@NonNull Status status) {
Toast.makeText(ActivityFenceActivity.this,
"Cannot unregister headphone fence.",
Toast.LENGTH_SHORT).show();
}
});
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.awareness.API_KEY"
android:value="AIzaSyBEmjlfC87xRUP2FnFynsDdY3QRuI1hIHs" />
</application>
您是否嘗試添加STILL Fence來檢查此柵欄是否被觸發? – MPhil
我遇到了同樣的問題。我試圖對用戶活動的變化做出反應,但不知何故,它只會觸發一次,然後再次觸發。你有沒有設法得到任何見解? – saberrider