我需要你的宝贵帮助;)
我有一个在后台运行的Android 服务,它定期修复设备的位置(位置轮询器)。当我使用使用 Google Play 服务的新 Android 位置 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)
你怎么看?我读了一个类似的帖子,但不是一回事。似乎有时如果 areServicesConnected() 方法返回 true,稍后服务将由于某些原因断开连接。
谢谢,每一个帮助将不胜感激!
快乐编码;)