울타리 만들기

펜스는 앱이 반응할 수 있는 하나 이상의 컨텍스트 조건을 정의합니다. 펜스 상태가 변경되면 앱이 콜백을 수신합니다.

펜스에는 두 가지 유형이 있습니다. 컨텍스트 신호의 기본 집합을 나타내는 원시 펜스와 여러 원시 펜스를 불리언 연산자와 결합하는 조합 펜스입니다. 모든 펜스는 AwarenessFence의 인스턴스입니다.

기본 펜스 만들기

기본 시그널 세트를 나타내는 기본 펜스는 awareness.fence 패키지에 정의되어 있습니다. 다음 예에서는 사용자의 감지된 활동이 WALKING일 때 TRUE이고 그렇지 않으면 FALSE인 간단한 펜스 생성을 보여줍니다.

AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);

위의 예에서 DetectedActivityFenceduring를 호출하여 생성되었으며 사용자가 WALKING일 때마다 펜스가 TRUE 상태입니다.

전환에 반응

각 기본 펜스 유형(TimeFence 제외)도 컨텍스트 상태가 전환될 때 잠시 트리거될 수 있습니다. 예를 들어 사용자가 starting 또는 stopping 활동일 때 일시적으로 트리거되도록 DetectedActivityFence를 설정할 수 있습니다. Transition 펜스는 몇 초 동안 TRUE 상태로 전환된 후 FALSE 상태가 됩니다.

조합 펜스 만들기

Combination 펜스는 여러 프리미티브 펜스 유형을 불리언 연산자와 함께 사용합니다. 다음 예는 사용자가 걷고 있고 헤드폰이 연결되어 있을 때 활성화되는 조합 펜스 생성을 보여줍니다.

// Create the primitive fences.
AwarenessFence walkingFence = DetectedActivityFence.during(DetectedActivityFence.WALKING);
AwarenessFence headphoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);

// Create a combination fence to AND primitive fences.
AwarenessFence walkingWithHeadphones = AwarenessFence.and(
      walkingFence, headphoneFence
);

AND, OR, NOT의 중첩 트리가 유효하므로 펜스의 모든 부울 조합이 가능합니다. 다음 예는 사용자가 현재 위치에서 100m 이상 이동하거나 현재 1시간 이상 경과했을 때 트리거되는 펜스를 보여줍니다.

double currentLocationLat;  // current location latitude
double currentLocationLng;  // current location longitude
long nowMillis = System.currentTimeMillis();
long  * 60L * 60L * 1000L;

AwarenessFence orExample = AwarenessFence.or(
        AwarenessFence.not(LocationFence.in(
                currentLocationLat,
                currentLocationLng,
                100.0,
                100.0,
                0L)),
        TimeFence.inInterval(nowMillis + oneHourMillis, Long.MAX_VALUE));

다음 단계: 펜스 등록