我有一個關於Android的一個非常奇怪的問題在Android上。我有一個應用程序使用com.google.android.gms.location.LocationClient並每15秒請求一次locationUpdate。它位於後臺服務中,用於跟蹤用戶的位置。幾乎所有時間都能正常工作,如果手機長時間處於沒有任何位置提供商(GPS,Wi-Fi,Cell ..)的地方(例如在地下室,車庫..),則會出現問題。 )。在離開那個地方並接收到一個新的位置後,整個設備會被阻塞,需要重新啓動(取出電池)才能繼續工作。您是否看到過這種行爲,您是否知道解決方法?與gps服務的Android應用程序阻止設備,直到重新啓動
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();
}
}
看起來很奇怪......當你說「後臺服務」你到底用了什麼? –
更新了我的答案 –
問題是,它不是應用程序崩潰...整個電話塊,直到重新啓動 –