我需要你宝贵的帮助;)
我有 Android服务,在后台运行定期修复设备的位置(位置轮询 STRONG>)。当我使用使用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,但不是一回事。似乎有时候如果方法isServicesConnected()返回true,稍后由于某些原因服务将被断开。
谢谢,非常感谢每一位帮助!
快乐的编码;)
答案 0 :(得分:6)
isGooglePlayServicesAvailable
并未告诉您已连接,只是检查服务的可用性。
你应该完全按照你的logcat做什么。致电LocationClient.connect()
,一旦您的代码到达ConnectionCallbacks.onConnected
的实施内容,您就会联系到我的问题。