我对 Android 上的 GPS 有一个非常奇怪的问题。我有一个使用 com.google.android.gms.location.LocationClient 并每 15 秒请求一次 locationUpdate 的应用程序。它托管在后台服务中,并正在跟踪用户的位置。几乎所有时间都能完美运行,如果手机长时间处于没有任何位置提供程序(GPS、Wi-Fi、Cell ..)的地方(例如地下室、车库......),就会出现问题。 )。离开那个地方并收到一个新位置后,整个设备会阻塞并需要硬重启(取出电池)才能继续工作。你见过这种行为吗?你知道解决方法吗?
startService(new Intent(this, GPSService.class));
这是服务:
public class GPSService extends Service implements LocationListener,
ConnectionCallbacks, OnConnectionFailedListener {
private LocationClient locationclient;
private LocationRequest locationrequest;
private void InitGpsService() {
if (locationclient != null && locationclient.isConnected()) {
return;
}
int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resp == ConnectionResult.SUCCESS) {
locationclient = new LocationClient(this, this, this);
locationclient.connect();
Log.d("Messangero", "Location Client Connect");
} else {
Toast.makeText(this, "Google Play Service Error " + resp,
Toast.LENGTH_LONG).show();
}
}
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
GPSService getService() {
// Return this instance of LocalService so clients can call public
// methods
return GPSService.this;
}
}
public IBinder onBind(Intent arg0) {
return mBinder;
}
public void onCreate() {
super.onCreate();
InitGpsService();
};
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
};
public void onDestroy() {
super.onDestroy();
if (locationclient != null && locationclient.isConnected()) {
locationclient.removeLocationUpdates(this);
locationclient.disconnect();
}
}
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
public void onConnected(Bundle arg0) {
// TODO Auto-generated method stub
PreferencesUtil.LoadSettings(this);
locationrequest = new LocationRequest();
locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationrequest.setInterval(PreferencesUtil.GPSSyncPeriod);
locationclient.requestLocationUpdates(locationrequest, this);
}
public void onDisconnected() {
// TODO Auto-generated method stub
if (locationclient != null && locationclient.isConnected())
locationclient.removeLocationUpdates(this);
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Thread thr = new Thread(new LocationUpdateThread(GPSService.this,
location));
thr.start();
}
}