2

我尝试使用 Google Awareness API 实现 Activity Fence。但是没有检测到用户活动的变化。耳机围栏按预期工作。

活动围栏活动

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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.subhrajyoti.awareness">

    <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>

</manifest>
4

2 回答 2

0

我尝试使用 Awareness API 而不是地理围栏,我认为问题在于您如何测试代码。我很确定 Awareness 使用手机的传感器,所以当我使用 GPS 模拟器模拟步行时,它不会触发任何东西,但如果我使用我的真实手机走路,则会触发 BroadcastReceiver。但是,我不知道如何在模拟器上模拟“传感器行走”!

于 2017-10-20T02:49:21.303 回答
0

如果耳机围栏正常工作而活动围栏不工作,也许您只是忘记在清单中添加权限?

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
于 2017-04-08T13:22:47.290 回答