【发布时间】:2013-10-08 03:43:05
【问题描述】:
我需要你的宝贵帮助;)
我有一个 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,稍后服务将由于某些原因断开连接。
谢谢,每一个帮助将不胜感激!
编码愉快 ;)
【问题讨论】:
标签: android geolocation android-service android-location