<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          自學HarmonyOS應用開發(fā)(58)- 接受來自Service的通知

          PageAbility和ServiceAbility之間的交互可以有兩種方式:一種是主動查詢,也就是自學HarmonyOS應用開發(fā)(57)- 與Service進行交互中介紹的方式,還有一種是被動接受來自ServiceAbility的通知。本文介紹后一種方式。

          涉及到的類比較多,關系比較復雜:


          接受通知的接口類

          首先在StopWatchServiceConnection類中定義接受ServiceAbilitty通知的接口類:

          public interface StopWatchEventListener{    void onConnectDone(StopWatchAgentProxy proxy);    void onDisconnectDone();    void onLocationReported();}

          三個方法分別用于接受服務連接/切斷和位置信息更新通知。

          接下來在MainAbilitySlice中定義實現(xiàn)StopWatchEventListener接口的listener:

          StopWatchServiceConnection.StopWatchEventListener listener = new StopWatchServiceConnection.StopWatchEventListener(){    @Override    public void onConnectDone(StopWatchAgentProxy proxy) {        stopWatchProxy = proxy;        ...    }
          @Override public void onDisconnectDone() { stopWatchProxy = null; }
          @Override public void onLocationReported() { ... }};

          此處省略實際的處理過程,而將重點放在如何建立和利用通知渠道上。


          建立通知渠道

          在MainAbilitySlice建立與StopWatchService之間連接時,同時將listener登錄到StopWatchConnection上:

          private void connectService() {    HiLog.info(LOG_LABEL, "MainAbilitySlice.connectService!");    Intent intent = getLocalServiceIntent(LOCAL_BUNDLE, FOREGROUND_SERVICE);    connection = new StopWatchServiceConnection();    connection.setStopWatchEventListener(listener);    connectAbility(intent, connection);}


          定位信息通知過程

          通過下圖說明地圖數(shù)據(jù)更新的過程:

          1. StopWatchService中的MyLocatorCallback接收到定位信息之后,獲取StopWatchConnection的handler示例

          2. MyLocatorCallback通過sendEvent向hander發(fā)送事件通知

          3. hander進行上下文切換之后,調(diào)用StopWatchEventListener的onLocationReported方法。而這個實例就是MainAbilitySlice事先登錄的那個。

          • StopWatchEventListener的onLocationReported中調(diào)用MainAbilitySlice的畫面更新方法。



          參考代碼

          完整代碼可以從以下鏈接下載:

          https://github.com/xueweiguo/Harmony/tree/master/StopWatch

          華為官方示例代碼:

          https://gitee.com/openharmony/app_samples/tree/master/ability/AbilityConnection


          作者著作介紹

          《實戰(zhàn)Python設計模式》是作者去年3月份出版的技術書籍,該書利用Python 的標準GUI 工具包tkinter,通過可執(zhí)行的示例對23 個設計模式逐個進行說明。這樣一方面可以使讀者了解真實的軟件開發(fā)工作中每個設計模式的運用場景和想要解決的問題;另一方面通過對這些問題的解決過程進行說明,讓讀者明白在編寫代碼時如何判斷使用設計模式的利弊,并合理運用設計模式。

          對設計模式感興趣而且希望隨學隨用的讀者通過本書可以快速跨越從理解到運用的門檻;希望學習Python GUI 編程的讀者可以將本書中的示例作為設計和開發(fā)的參考;使用Python 語言進行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書中的示例為基礎,迅速構(gòu)建自己的系統(tǒng)架構(gòu)。




          覺得本文有幫助?請分享給更多人。

          關注微信公眾號【面向?qū)ο笏伎肌枯p松學習每一天!

          面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>



          瀏覽 26
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天爽夜夜爽AA片免费 | 日韩AV东京热一 | 青青草在线视频免费在线观看 | 2017AV天堂网 | 婷婷亚洲天堂 |