久久久久久久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 上服務(wù)的良好做法

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

        <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 上服務(wù)的良好做法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我目前在我的應(yīng)用中使用 2 項服務(wù):

                  I am currently using 2 services in my app:

                  1:LocationService,主要是嘗試本地化用戶,目標(biāo)是僅在應(yīng)用處于前臺時才保持活動狀態(tài).

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

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

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

                  我已經(jīng)閱讀了很多文檔,但我無法說清楚.

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

                  當(dāng)我嘗試存儲 LocationServiceBinder 的引用時,我遇到了泄漏,它用于調(diào)用我的服務(wù)函數(shù)(使用 AIDL 接口).Xmpp 也一樣.當(dāng)我解除綁定時,有時會出現(xiàn) ANR(這似乎與我的綁定/解除綁定異常完成的事實有關(guān),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 ...).

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

                  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 ! :)

                  干杯

                  更新

                  我的位置服務(wù)在應(yīng)用啟動時綁定,以盡可能快地獲取用戶的位置:

                  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 服務(wù)在用戶登錄時啟動:

                  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;
                      }
                  

                  推薦答案

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

                  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() 中調(diào)用 MyServiceBinder.doSomething(),或者通過一些用戶交互(例如按鈕單擊)執(zhí)行 MyServiceBinder.doSomething(),因為在調(diào)用 bindService() 之后和系統(tǒng)獲取之前的滯后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.

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

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

                  相關(guān)文檔推薦

                  Help calculating X and Y from Latitude and Longitude in iPhone(幫助從 iPhone 中的緯度和經(jīng)度計算 X 和 Y)
                  Get user#39;s current location using GPS(使用 GPS 獲取用戶的當(dāng)前位置)
                  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 或網(wǎng)絡(luò)提供商)

                    <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. 主站蜘蛛池模板: 亚洲精品一区二区三区中文字幕 | 欧美精品一区二区三区在线播放 | 羞羞视频免费在线观看 | 在线视频一区二区三区 | 日本久久精品视频 | 久久只有精品 | 国产精品视频一区二区三区四蜜臂 | 国产精品免费一区二区三区四区 | 亚洲国产精品一区二区三区 | 免费观看a级毛片在线播放 黄网站免费入口 | 999精品视频| 久久久久久久久国产精品 | av看片| 色橹橹欧美在线观看视频高清 | 综合久久综合久久 | 麻豆精品久久 | 污污的网站在线观看 | 一级a爱片久久毛片 | 日韩电影一区二区三区 | wwwww在线观看 | 国产人久久人人人人爽 | 午夜精品一区二区三区在线 | 激情福利视频 | 手机看黄av免费网址 | 精品欧美激情精品一区 | 爱高潮www亚洲精品 中文字幕免费视频 | 亚洲色欲色欲www | 国产精品18久久久久久久 | 欧美精选一区二区 | 成人av在线网站 | 久久久久国产精品一区三寸 | 欧美99| 国产乱码一二三区精品 | 久久av网 | 欧美成人免费在线 | 国产成人综合在线 | 久久一及片 | 中文亚洲视频 | 综合久久av | 精品在线一区 | 午夜精品久久久 |