久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='6UoeV'><tr id='6UoeV'><dt id='6UoeV'><q id='6UoeV'><span id='6UoeV'><b id='6UoeV'><form id='6UoeV'><ins id='6UoeV'></ins><ul id='6UoeV'></ul><sub id='6UoeV'></sub></form><legend id='6UoeV'></legend><bdo id='6UoeV'><pre id='6UoeV'><center id='6UoeV'></center></pre></bdo></b><th id='6UoeV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6UoeV'><tfoot id='6UoeV'></tfoot><dl id='6UoeV'><fieldset id='6UoeV'></fieldset></dl></div>
    <bdo id='6UoeV'></bdo><ul id='6UoeV'></ul>

  • <small id='6UoeV'></small><noframes id='6UoeV'>

      1. <tfoot id='6UoeV'></tfoot>

      2. <legend id='6UoeV'><style id='6UoeV'><dir id='6UoeV'><q id='6UoeV'></q></dir></style></legend>

      3. Android 上服務的良好做法

        Good practices for services on Android(Android 上服務的良好做法)

        <small id='keOfe'></small><noframes id='keOfe'>

            <tbody id='keOfe'></tbody>
          <tfoot id='keOfe'></tfoot>

            <i id='keOfe'><tr id='keOfe'><dt id='keOfe'><q id='keOfe'><span id='keOfe'><b id='keOfe'><form id='keOfe'><ins id='keOfe'></ins><ul id='keOfe'></ul><sub id='keOfe'></sub></form><legend id='keOfe'></legend><bdo id='keOfe'><pre id='keOfe'><center id='keOfe'></center></pre></bdo></b><th id='keOfe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='keOfe'><tfoot id='keOfe'></tfoot><dl id='keOfe'><fieldset id='keOfe'></fieldset></dl></div>
            <legend id='keOfe'><style id='keOfe'><dir id='keOfe'><q id='keOfe'></q></dir></style></legend>

                • <bdo id='keOfe'></bdo><ul id='keOfe'></ul>
                  本文介紹了Android 上服務的良好做法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我目前在我的應用中使用 2 項服務:

                  I am currently using 2 services in my app:

                  1:LocationService,主要是嘗試本地化用戶,目標是僅在應用處于前臺時才保持活動狀態.

                  1: LocationService, basically trying to localize the user, and aims to stay alive only when the app is on foreground.

                  2:XmppService,它初始化與 xmpp 服務器的連接、接收消息、發送消息、注銷...并旨在保持活動狀態直到用戶注銷.

                  2: XmppService, which init the connection with the xmpp server, receive messages, send it, logout ... and aims to stay alive until the user logout.

                  我已經閱讀了很多文檔,但我無法說清楚.

                  I've been reading quite a lot of documentation, but I just can't make it clear.

                  當我嘗試存儲 LocationServiceBinder 的引用時,我遇到了泄漏,它用于調用我的服務函數(使用 AIDL 接口).Xmpp 也一樣.當我解除綁定時,有時會出現 ANR(這似乎與我的綁定/解除綁定異常完成的事實有關,onResume、onRestart ...).

                  I'm having Leaks when I try to store reference of LocationServiceBinder, which is used to call my service functions (using AIDL interfaces). Same for Xmpp. When I unbind, I get sometimes ANR (which look like to be linked with the fact that my bind/unbind are weirdly done, onResume, onRestart ...).

                  所有系統都在工作,但我確信這不是正確的做法,我很樂意跟隨有經驗的人回到部隊的右側!:)

                  All the system is working, but I'm sure it is not the right way to do it, and please I would love to follow experienced people to come back in the right side of the force ! :)

                  干杯

                  更新

                  我的位置服務在應用啟動時綁定,以盡可能快地獲取用戶的位置:

                  My Location Service is bind at the app launch to get as fast as possible the user's position :

                  if(callConnectService == null) {
                              callConnectService = new ServiceConnection() {
                                  public void onServiceConnected(ComponentName name, IBinder binder) {
                                      locationServiceBinder = LocationServiceBinder.Stub.asInterface(binder);
                                      try {
                                          global.setLocationBinder(locationServiceBinder); 
                                          global.getLocationBinder().startLocationListener();
                                      } catch (Exception e){
                                          Log.e(TAG, "Service binder ERROR");
                                      }
                                  }
                  
                                  public void onServiceDisconnected(ComponentName name) {
                                      locationServiceBinder = null;
                                  }
                              };
                          }
                  
                          /* Launch Service */
                          aimConServ =  new Intent(this, LocationService.class);
                          boolean bound = bindService(aimConServ,callConnectService,BIND_AUTO_CREATE);
                  

                  我的 Xmpp 服務在用戶登錄時啟動:

                  My Xmpp Service is launched when the user log in :

                  callConnectService = new ServiceConnection() {

                  callConnectService = new ServiceConnection() {

                              public void onServiceConnected(ComponentName name, IBinder binder) {
                                  try {
                                      Log.d(TAG, "[XMPP_INIT] Complete.");
                                      global.setServiceBinder(ConnectionServiceBinder.Stub.asInterface(binder)); 
                                      //Connect to XMPP chat
                                      global.getServiceBinder().connect();
                                  } catch (Exception e){
                                      Log.e(TAG, "Service binder ERROR ");
                                      e.printStackTrace();
                                  }
                              }
                  
                              public void onServiceDisconnected(ComponentName name) {
                                  Log.e(TAG, "Service binder disconnection ");
                              }
                          };
                  
                          /* Launch Service */
                          Intent aimConServ =  new Intent(MMWelcomeProfile.this, XmppService.class);
                          bound = bindService(aimConServ,callConnectService,Context.BIND_AUTO_CREATE);
                  

                  并在每個 Activity 上取消綁定:

                  and unbind on each Activity :

                  if (callConnectService != null){
                          unbindService(callConnectService);
                          callConnectService = null;
                      }
                  

                  推薦答案

                  在谷歌官方開發指南中并沒有詳細記載,Context.bindService() 實際上是一個異步調用.這就是為什么 ServiceConnection.onServiceConnected() 被用作回調方法的原因,意味著不會立即發生.

                  It hasn't been well-documented in Google's official dev guide, Context.bindService() is actually an asynchronous call. This is the reason why ServiceConnection.onServiceConnected() is used as a callback method, means not happened immediately.

                  public class MyActivity extends Activity {
                    private MyServiceBinder myServiceBinder;
                  
                    protected ServiceConnection myServiceConnection = new ServiceConnection() {
                      public void onServiceConnected(ComponentName className, IBinder service) {
                        myServiceBinder = (MyServiceBinderImpl) service;
                      }
                  
                      ... ...
                    }
                  
                    public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      // bindService() is an asynchronous call. myServiceBinder is resoloved in onServiceConnected()
                      bindService(new Intent(this, MyService.class),myServiceConnection, Context.BIND_AUTO_CREATE);
                      // You will get a null point reference here, if you try to use MyServiceBinder immediately.
                      MyServiceBinder.doSomething(); // <-- not yet resolved so Null point reference here
                    }
                  }
                  

                  解決方法是在 myServiceConnection.onServiceConnected() 中調用 MyServiceBinder.doSomething(),或者通過一些用戶交互(例如按鈕單擊)執行 MyServiceBinder.doSomething(),因為在調用 bindService() 之后和系統獲取之前的滯后myServiceBinder 的引用很快.只要你不立即使用它就可以了.

                  A workaround is call MyServiceBinder.doSomething() in myServiceConnection.onServiceConnected(), or perform MyServiceBinder.doSomething() by some user interaction (e.g. button click), as the lag after you call bindService() and before system get a reference of myServiceBinder is quite soon. as long as you are not using it immediately, you should be just fine.

                  查看這個 SO 問題CommonsWare 的答案了解更多詳情.

                  Check out this SO question CommonsWare's answer for more details.

                  這篇關于Android 上服務的良好做法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Help calculating X and Y from Latitude and Longitude in iPhone(幫助從 iPhone 中的緯度和經度計算 X 和 Y)
                  Get user#39;s current location using GPS(使用 GPS 獲取用戶的當前位置)
                  IllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 拋出的 IllegalArgumentException)
                  How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新一次?)
                  CLLocation returning negative speed(CLLocation 返回負速度)
                  How to detect Location Provider ? GPS or Network Provider(如何檢測位置提供者?GPS 或網絡提供商)

                    <small id='Cm0PE'></small><noframes id='Cm0PE'>

                  • <i id='Cm0PE'><tr id='Cm0PE'><dt id='Cm0PE'><q id='Cm0PE'><span id='Cm0PE'><b id='Cm0PE'><form id='Cm0PE'><ins id='Cm0PE'></ins><ul id='Cm0PE'></ul><sub id='Cm0PE'></sub></form><legend id='Cm0PE'></legend><bdo id='Cm0PE'><pre id='Cm0PE'><center id='Cm0PE'></center></pre></bdo></b><th id='Cm0PE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Cm0PE'><tfoot id='Cm0PE'></tfoot><dl id='Cm0PE'><fieldset id='Cm0PE'></fieldset></dl></div>
                    <legend id='Cm0PE'><style id='Cm0PE'><dir id='Cm0PE'><q id='Cm0PE'></q></dir></style></legend>

                    • <bdo id='Cm0PE'></bdo><ul id='Cm0PE'></ul>
                        <tfoot id='Cm0PE'></tfoot>
                          <tbody id='Cm0PE'></tbody>

                          1. 主站蜘蛛池模板: 99re视频在线 | 国产精品一级 | 国产日韩欧美激情 | 天堂网avav | 亚洲444eee在线观看 | 中文字幕欧美日韩一区 | 99欧美精品 | 亚洲一区二区三区视频免费观看 | 在线观看 亚洲 | 日本高清视频在线播放 | 久久av网 | 亚洲大片 | 久久久久免费精品国产小说色大师 | 99精品欧美一区二区蜜桃免费 | 中文字幕日韩欧美一区二区三区 | 国产 日韩 欧美 中文 在线播放 | 一区二区三区亚洲 | 欧美一区二区三区在线观看视频 | 日韩aⅴ在线观看 | 看黄在线 | 亚洲一区在线日韩在线深爱 | 久久久精品网站 | 国产精品免费一区二区三区四区 | 日韩欧美一区二区三区免费看 | 久久er精品 | 91在线影院 | 色综合一区二区 | 激情一区二区三区 | 国产亚洲一区二区三区在线观看 | 亚洲二区视频 | 精品国产区 | 一区二区免费在线 | 狠狠伊人 | 天天久久 | 国产乱一区二区三区视频 | 日韩一区二区三区在线 | 亚洲一区二区国产 | 精品国产精品国产偷麻豆 | 老熟女毛片 | 国产乱码精品一区二三赶尸艳谈 | 国产成人精品一区二区三 |