Share via


在背景接聽地理柵欄事件 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

本主題將逐步引導您完成在應用程式中設定背景工作以接聽 Geofence 通知的步驟。

藍圖: 這個主題與其他主題的相關性?請參閱:

簡介

一旦建立您的地理柵欄之後,就必須加入邏輯,以處理發生地理柵欄事件時所發生的情況。 根據您已經設定的 MonitoredStates,您可能會在下列情況下收到事件:

  • 使用者進入相關的區域時。
  • 使用者離開相關的區域時。
  • 地理柵欄過期或遭到移除時。請注意,系統不會啟動移除事件的背景應用程式。

本主題說明如何設定背景工作,以便在地理柵欄事件發生時,向您的應用程式發出警示。 但是您也可以在應用程式執行時,直接從應用程式中處理事件。 如需詳細資訊,請參閱在前景處理地理柵欄通知地理柵欄的指導方針

在背景接聽地理柵欄事件需要數個步驟:

  • 在應用程式的資訊清單中宣告背景工作
  • 在應用程式中登錄背景工作。如果您的應用程式需要存取網際網路,比方說在觸發事件時存取雲端服務,您可以針對這種情況設定一個旗標。 您也可以設定一個旗標,確保觸發事件時使用者在場,以確定使用者確實收到通知。
  • 當您的應用程式在前景執行時,提示使用者授予您的應用程式位置權限。

登錄地理柵欄狀態變更事件

在您應用程式資訊清單的 [宣告] 索引標籤底下,新增位置背景工作的宣告。若要這樣做:

  • 新增 [背景工作]**** 類型的宣告。
  • 設定 [位置] 的屬性工作類型。
  • 在您的應用程式中設定一個進入點,以便在觸發事件時呼叫。

登錄背景工作

下列程式碼會登錄地理柵欄背景工作。請記住,在地理柵欄建立之初時,我們即已檢查過位置權限。 如需詳細資訊,請參閱設定地理柵欄

function registerBackgroundTask() {
    try {
        // Request lockscreen access
        Background.BackgroundExecutionManager.requestAccessAsync().done(
            function (backgroundAccessStatus) {
                var builder =  new Windows.ApplicationModel.Background.BackgroundTaskBuilder();

                // Register the background task
                builder.name = sampleBackgroundTaskName;
                builder.taskEntryPoint = sampleBackgroundTaskEntryPoint;
                builder.setTrigger(new Windows.ApplicationModel.Background.LocationTrigger(Windows.ApplicationModel.Background.LocationTriggerType.geofence));

                // If it is important that there is user presence and/or
                // internet connection when OnCompleted is called
                // the following could be called before calling Register()
                // var condition = new SystemCondition(SystemConditionType.userPresent | SystemConditionType.internetAvailable);
                // builder.addCondition(condition);

                geofenceTask = builder.register();

                geofenceTask.addEventListener("completed", onCompleted);

                LocationTriggerBackgroundTask.updateButtonStates(/*registered:*/ true);

                switch (backgroundAccessStatus) {
                    case Background.BackgroundAccessStatus.unspecified:
                    case Background.BackgroundAccessStatus.denied:
                        WinJS.log && WinJS.log("This app is not allowed to run in the background.", "sample", "status");
                        break;

                    default:
                        // Finish by getting an initial position. This will present the location consent
                        // dialog if it's the first attempt for this application to access location.
                        getGeopositionAsync();
                        break;
                }
            },
            function (e) {
                // Did you forget to do the background task declaration in the package manifest?
                WinJS.log && WinJS.log(e.toString(), "sample", "error");
            }
        );
    } catch (ex) {
        // HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) === -2147024846

        if (ex.number === -2147024846) {
            WinJS.log && WinJS.log("Location Simulator not supported.  Could not get permission to add application to the lock screen, this application must be added to the lock screen before the background task will run.", "sample", "status");
        } else {
            WinJS.log && WinJS.log(ex.toString(), "sample", "error");
        }
    }
}

相關主題

藍圖

使用 JavaScript 建立應用程式的藍圖

設計應用程式的 UX

工作

設定地理柵欄

在前景處理地理柵欄通知

從背景工作處理地理柵欄通知

參考

Geoshape

Geofence

Geolocator

其他資源

Windows 10 地理位置範例

Windows 8.1 地理位置範例

地理柵欄的指導方針