本部分介绍了如何使用 Snapshot API 获取每种受支持的情境类型的当前状态。如需了解详情,请参阅使用入门。如需详细了解已弃用的情境信号,请打开以下可展开的通知:
获取当前 activity
如需获取用户的当前 activity,请调用 getDetectedActivity(),该方法会返回一个 ActivityRecognitionResult,其中包含有关用户最近 activity 的信息。
getDetectedActivity() 方法需要 com.google.android.gms.permission.ACTIVITY_RECOGNITION 权限。将此权限添加到 AndroidManifest.xml。
如需获取用户的当前 activity,请执行以下步骤:
- 调用 getSnapshotClient()以创建SnapshotClient的实例。
- 使用 addOnSuccessListener创建一个可以监听DetectedActivityResponse的OnSuccessListener。
- 调用 getStatus()以确保结果有效。
- 调用 - DetectedActivityResponse.getActivityRecognitionResult()以返回- ActivityRecognitionResult。您可以使用此方法获取用户当前活动的许多方面。例如:- 调用 getMostProbableActivity()可仅获取最可能的 activity。
- 调用 getProbableActivities()可获取按概率排序的近期活动列表。
- 调用 getActivityConfidence()可返回指定活动类型的置信度值。
- 调用 hasResult()以检测Intent是否包含ActivityRecognitionResult。
 
- 调用 
以下代码示例使用 getMostProbableActivity() 获取最有可能检测到的 activity,并将结果记录到控制台:
Awareness.getSnapshotClient(this).getDetectedActivity()
    .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
        @Override
        public void onSuccess(DetectedActivityResponse dar) {
            ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
            DetectedActivity probableActivity = arr.getMostProbableActivity();
            int confidence = probableActivity.getConfidence();
            String activityStr = probableActivity.toString();
            mLogFragment.getLogView().println("Activity: " + activityStr
                + ", Confidence: " + confidence + "/100");
        }
    })
获取附近的 Beacon
如需获取有关附近信标的信息,请调用 getBeaconState()。
Beacon 数据包含任何附件的内容、类型和命名空间。
getBeaconState() 方法需要 android.permission.ACCESS_FINE_LOCATION 权限。将此权限添加到 AndroidManifest.xml。
此外,您还必须为 Google Developers Console 项目激活 Nearby Messages API。如需了解详情,请参阅注册和 API 密钥以及使用入门。
如需获取有关附近 Beacon 的信息,请执行以下步骤:
- 检查用户是否已授予所需权限。以下示例检查是否已授予 - android.permission.ACCESS_FINE_LOCATION权限。如果未获得,系统会提示用户授予同意声明。- if (ContextCompat.checkSelfPermission( MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION ); return; }
- 定义 - BeaconState.TypeFilter。 此方法仅返回已注册指定命名空间和类型的附件的 Beacon。您还可以根据附件内容的逐字节匹配结果进行过滤。以下示例展示了如何创建类型过滤条件:- private static final List<BeaconState.TypeFilter> BEACON_TYPE_FILTERS = Arrays.asList( BeaconState.TypeFilter.with( "my.beacon.namespace", "my-attachment-type"), BeaconState.TypeFilter.with( "my.other.namespace", "my-attachment-type"));
- 使用 - addOnSuccessListener创建一个可以监听- BeaconStateResponse的- OnSuccessListener。
- 调用 - getStatus()以确保结果有效。
- 调用 - BeaconStateResponse.getBeaconState()可返回 beacon 状态。
以下示例展示了如何获取信标信息:
Awareness.getSnapshotClient(this).getBeaconState(BEACON_TYPE_FILTERS)
    .addOnSuccessListener(new OnSuccessListener<BeaconStateResponse>() {
        @Override
        public void onSuccess(BeaconStateResponse beaconStateResponse) {
            BeaconStateResult beaconStateResult = beaconStateResponse.getBeaconState();
            BeaconState.BeaconInfo beaconInfo = beaconStateResponse.getBeaconInfo();
        }
    })
获取耳机状态
如需检测耳机是否已插入设备,请调用 getHeadphoneState(),该方法会创建一个检测状态为 OnSuccessListener 的 HeadphoneStateResponse 检测状态。然后,您可以调用 getHeadphoneState() 来获取 HeadphoneState。
如需获取当前的耳机状态,请执行以下步骤:
- 调用 getSnapshotClient.getHeadphoneState()。
- 使用 addOnSuccessListener创建一个可以监听HeadphoneStateResponse的OnSuccessListener。
- 调用 getStatus()以确保结果有效。
- 成功后,调用 HeadphoneStateResponse.getHeadphoneState()以返回耳机状态。此值为PLUGGED_IN或UNPLUGGED。
以下代码示例展示了如何使用 getHeadphoneState():
Awareness.getSnapshotClient(this).getHeadphoneState()
    .addOnSuccessListener(new OnSuccessListener<HeadphoneStateResponse>() {
        @Override
        public void onSuccess(HeadphoneStateResponse headphoneStateResponse) {
            HeadphoneState headphoneState = headphoneStateResponse.getHeadphoneState();
            boolean pluggedIn = headphoneState.getState() == HeadphoneState.PLUGGED_IN;
            String stateStr =
                "Headphones are " + (pluggedIn ? "plugged in" : "unplugged");
            mLogFragment.getLogView().println(stateStr);
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Could not get headphone state: " + e);
        }
    });
获取位置
您可以调用 getLocation() 来获取用户的当前位置(纬度-经度),该方法会返回一个 LocationResponse。然后,您可以调用 LocationResponse.getLocation() 以获取包含当前位置数据的 Location。
getLocation() 方法需要 android.permission.ACCESS_FINE_LOCATION 权限。将此权限添加到 AndroidManifest.xml。
如需获取当前位置,请执行以下步骤:
- 检查用户是否已授予所需权限。以下示例检查了是否已授予 - android.permission.ACCESS_FINE_LOCATION权限。如果未获得,系统会提示用户授予同意声明。- if (ContextCompat.checkSelfPermission( MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_LOCATION ); return; }
- 使用 - addOnSuccessListener创建一个可以监听- LocationResponse的- OnSuccessListener。
- 调用 - getStatus()以确保结果有效。
- 调用 - LocationResponse.getLocation()可返回当前的- Location。
以下示例展示了如何获取当前位置:
Awareness.getSnapshotClient(this).getLocation()
    .addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
        @Override
        public void onSuccess(LocationResponse locationResponse) {
            Location loc = locationResponse.getLocationResult();
        }
    })