快速入門:處理連絡人動作 (HTML)

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

針對數個連絡人動作 (Windows.ApplicationModel.Contacts.ContactLaunchActionVerbs) 啟用應用程式時,您可以透過 Windows.UI.WebUIWindows.ApplicationModel.Activation 命名空間提供資料給應用程式。這裡,我們將說明當使用者嘗試撥打電話給連絡人、傳送訊息給連絡人或在地圖尋找連絡人地址時,如何處理應用程式啟用。這些應用程式啟用動作可從 Windows Search 體驗或應用程式內擷取的連絡人卡片執行。您可以使用 ContactManager.ShowContactCardContactManager.ShowDelayLoadedContactCard 方法在應用程式內顯示連絡人卡片。從 Windows 8.1 開始為連絡人動作提供處理應用程式啟用的支援。

我們將在這裡參考處理連絡人動作範例。這個範例示範如何透過 Windows 市集應用程式內的 Windows.UI.WebUI 命名空間 API 為連絡人動作處理應用程式啟用。

此範例提供三種案例:

  1. 處理啟用以撥打電話 (ContactLaunchActionVerbs.call)
  2. 處理啟用以傳送訊息 (ContactLaunchActionVerbs.message)
  3. 處理啟用以在地圖尋找地址 (ContactLaunchActionVerbs.map)

先決條件

  • 我們建議您熟悉 Microsoft Visual Studio 以及相關範本。
  • 我們建議您先熟悉 JavaScript 開發。

包含應用程式需要為每個動作提供支援的資訊清單登錄

在 AppxManifest.xml 或 Package.appxmanifest 檔案中,更新 Package 元素以接受 Windows 8.1 資訊清單結構描述,並包含應用程式需要為每個動作提供支援的資訊清單登錄。這些登錄可在任何連絡人動作或通訊協定配置發生時啟動應用程式。

<Package xmlns="https://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="https://schemas.microsoft.com/appx/2013/manifest">
.
.
      <Extensions>
        <Extension Category="windows.protocol">
          <Protocol Name="tel"/>
        </Extension>        
        <m2:Extension Category="windows.contact">
          <m2:Contact>
            <m2:ContactLaunchActions>
              <m2:LaunchAction Verb="call">
                <m2:ServiceId>telephone</m2:ServiceId>
              </m2:LaunchAction>
              <m2:LaunchAction Verb="message">
                <m2:ServiceId>skype.com</m2:ServiceId>
              </m2:LaunchAction>            
              <m2:LaunchAction Verb="map"/>
            </m2:ContactLaunchActions>
          </m2:Contact>
        </m2:Extension>
      </Extensions>      

判斷要採用的案例

contactprotocol Windows.ApplicationModel.Activation.ActivationKind 啟用類型發生時啟動應用程式。

要採用的案例取決於所執行的連絡人動作。例如,如果使用者在連絡人電話號碼按一下 [撥號],事件動作為 ContactLaunchActionVerbs.call,而要採用的案例為 S1-Call

由於我們建議實作 Windows.Contact.call 動作的應用程式也實作 tel: protocol 的支援,因此偵測到 protocol Windows.ApplicationModel.Activation.ActivationKind 時也要採用 S1-Call 案例。

    function activated(eventObject) {
        var url = null;
        var arg = null;

        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.contact) {
            // If activated for a contact, launch the appropriate action handling scenario.
            arg = eventObject.detail;
            if (eventObject.detail.verb === Windows.ApplicationModel.Contacts.ContactLaunchActionVerbs.call) {
                url = scenarios[0].url;
            } else if (eventObject.detail.verb === Windows.ApplicationModel.Contacts.ContactLaunchActionVerbs.message) {
                url = scenarios[1].url;
            } else if (eventObject.detail.verb === Windows.ApplicationModel.Contacts.ContactLaunchActionVerbs.map) {
                url = scenarios[2].url;
            } else {
                WinJS.log && WinJS.log("This app can't handle the contact action verb it was activated for.", "sample", "error");
                return;
            }
        } else if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.protocol) {
            // If activated for a protocol, launch the call scenario
            arg = eventObject.detail;
            url = scenarios[0].url;
        } else if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
            // Otherise, navigate to either the first scenario or to the last running scenario
            // before suspension or termination.
            url = WinJS.Application.sessionState.lastUrl || scenarios[0].url;
        }

        if (url !== null) {
            // Use setPromise to indicate to the system that the splash screen must not be torn down
            // until after processAll and navigate complete asynchronously.
            eventObject.setPromise(WinJS.UI.processAll().then(function () {
                return WinJS.Navigation.navigate(url, arg);
            }));
        }
    }

    WinJS.Navigation.addEventListener("navigated", function (eventObject) {
        var url = eventObject.detail.location;
        var host = document.getElementById("contentHost");
        // Call unload method on current scenario, if there is one
        host.winControl && host.winControl.unload && host.winControl.unload();
        WinJS.Utilities.empty(host);
        eventObject.detail.setPromise(WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {
            WinJS.Application.sessionState.lastUrl = url;
        }));
    });

透過來電啟用應用程式時向應用程式提供資料

判斷我們收到的是連絡人來電事件,還是 tel: protocol 來電事件。

這裡針對連絡人來電事件傳送的資料為 ServiceIdServiceUserId。例如,此範例使用選定連絡人的電話號碼並顯示「收到來電啟用。要撥打的電話號碼為 (555) 555-0100」。

這裡針對 tel: protocol 來電事件傳送的資料為 URI schemeNamepath。例如,此範例使用選定連絡人的電話號碼並顯示「收到電話啟用。要撥打的電話號碼為 (555) 555-0100」。

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/S1-Call.html", {
        processed: function (element, callArgs) {
            // callArgs is the parameter passed to navigation in the activated event handler.
            if (callArgs) {
                if (callArgs.serviceId) {
                    if (callArgs.serviceId === "telephone") {
                        WinJS.log && WinJS.log("Call activation was received. The phone number to call is " + callArgs.serviceUserId + ".", "sample", "status");
                    } else {
                        WinJS.log && WinJS.log("This app doesn't support calling by using the " + callArgs.serviceId + " service.", "sample", "error");
                    }
                } else if (callArgs.uri) {
                    if (callArgs.uri.schemeName === "tel") {
                        WinJS.log && WinJS.log("Tel: activation was received. The phone number to call is " + callArgs.uri.path + ".", "sample", "status");
                    } else {
                        WinJS.log && WinJS.log("This app doesn't support the " + callArgs.uri.schemeName + " protocol.", "sample", "error");
                    }
                }
            }
        },
        ready: function (element, options) {
        }
    });
})();

透過傳送的訊息啟用應用程式時向應用程式提供資料

這裡針對連絡人傳送訊息事件傳送的資料為 ServiceIdServiceUserId。例如,此範例使用特定服務的連絡人使用者識別碼並顯示「收到傳送訊息啟用。要使用的服務為 contoso.com。接收訊息的目標使用者識別碼為 userid10」。

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/S2-Send-Message.html", {
        processed: function (element, messageArgs) {
            // messageArgs is the parameter passed to navigation in the activated event handler.
            if (messageArgs) {
                WinJS.log && WinJS.log("Send message activation was received. The service to use is " + messageArgs.serviceId + ". The user ID to message is " + 
                messageArgs.serviceUserId + ".", "sample", "status");
            }
        },
        ready: function (element, options) {
        }
    });
})();

透過地圖要求啟用應用程式時向應用程式提供資料

這裡針對連絡人地圖要求事件傳送的資料為 ContactAddress。例如,此範例使用連絡人的街道地址並顯示「收到在地圖尋找地址啟用。要在地圖尋找的街道地址為 One Microsoft Way」。

(function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/S3-Map-Address.html", {
        processed: function (element, mapArgs) {
            // mapArgs is the parameter passed to navigation in the activated event handler.
            if (mapArgs) {
                var address = mapArgs.address;
                WinJS.log && WinJS.log("Map address activation was received. The street address to map is " +
                    (address.streetAddress ? address.streetAddress : "unspecified") + ".", "sample", "status");
            }
        },
        ready: function (element, options) {
        }
    });
})();

摘要與後續步驟

現在您對於如何為連絡人動作處理應用程式啟用已經有基本的了解。從程式碼庫下載處理連絡人動作範例,查看如何為連絡人動作處理應用程式啟用的完整範例。

相關主題

處理連絡人動作範例