2013-10-01 22 views
1

我需要你的寶貴的幫助;)新位置API Android。錯誤「調用connect()和等待onConnected()被稱爲」

我有一個的Android服務在後臺運行是固定的了位置設備定期(一個位置輪詢器)。當我通過使用Google Play服務的new Android Location API更新了代碼時。讓我們看看到服務:

public class NewLocationPollerService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener 
{ 
    private LocationRequest mLocationRequest = null; 
    private LocationClient mLocationClient  = null; 

    ... 
    private class PollerThread extends WakefulThread 
    { 
     protected void onPreExecute() 
     { 
      if (GooglePlayServicesUtility.areServicesConnected(NewLocationPollerService.this)) 
       mLocationClient.requestLocationUpdates(mLocationRequest, intent); 
     } 

     protected void onPostExecute() 
     { 
      if (GooglePlayServicesUtility.areServicesConnected(NewLocationPollerService.this)) 
       mLocationClient.removeLocationUpdates(intent); 

      super.onPostExecute(); 
     } 
    } 
    ... 
} 

的方法 「areServicesConnected()」 如下:

public class GooglePlayServicesUtility 
{ 
    private static final String TAG = GooglePlayServicesUtility.class.getSimpleName(); 

    public static boolean areServicesConnected(Context context) 
    { 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); 
     if (ConnectionResult.SUCCESS == resultCode) 
      return true; 
     else 
      return false; 
    } 
    ... 
} 

有時,服務崩潰和日誌如下:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called. 
at com.google.android.gms.internal.k.B(Unknown Source) 
at com.google.android.gms.internal.bh.a(Unknown Source) 
at com.google.android.gms.internal.bh$c.B(Unknown Source) 
at com.google.android.gms.internal.bg.requestLocationUpdates(Unknown Source) 
at com.google.android.gms.internal.bh.requestLocationUpdates(Unknown Source) 
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source) 
at  me.app.location.NewLocationPollerService$PollerThread.onPreExecute(NewLocationPollerService.java:210 ) 
at me.app.thread.WakefulThread.onLooperPrepared(WakefulThread.java:79) 
at android.os.HandlerThread.run(HandlerThread.java:59) 
at me.app.thread.WakefulThread.run(WakefulThread.java:94) 

你覺得呢?我讀了similar post,但不是一回事。看來有時候,如果方法areServicesConnected()返回true,稍後由於某些原因服務將被斷開連接。

謝謝,每一個幫助將非常感激!

快樂編碼;)

回答

6

isGooglePlayServicesAvailable不告訴你,你連接,它只是檢查服務的可用性。

你應該完全做你的logcat。致電LocationClient.connect(),一旦您的代碼達到您的問題中沒有看到的實施ConnectionCallbacks.onConnected,您就會連接。

+0

你有沒有關於如何實現onconnected的例子?檢查您的服務是否已連接 – Cristiana214