我正在開發Android的另一個GPS跟蹤應用程序,我正在使用Android 4.3測試我的代碼在Sony Xperia SP c5302中。Android設備需要重新啓動/重新啓動GPS芯片長時間後才能獲取GPS位置
我做了一個後臺服務,使用Google Play服務的GoogleApiClient獲取融合位置。我能夠很好的獲得地點,但幾個小時後,我意識到我的GPS芯片停止返回地點,因爲他們的準確度從10提高到30或50(這意味着除GPS以外的其他提供商正在返回新地點)。
考慮到我在室內,我認爲API決定只聽網絡提供商,然後我去街上(無線和數據連接關閉),並停止獲取熔合位置(我沒有得到網絡如預期的那樣修復了並且GPS芯片已經如所述那樣死了)。即使Google地圖也無法追蹤我。然後我決定在那個時候重新啓動我的手機,並且我的應用程序和Google地圖都再次獲得了GPS芯片更新(無線和數據連接仍然關閉)。
我嘗試了與使用LocationManager的原始位置API使用相同的結果相同的方法....我必須重新啓動手機才能使GPS芯片再次工作。
這是我的基本代碼與GoogleApiClient:
public class MyService extends Service {
private final static String TAG = MyService.class.getSimpleName();
//GPS
private GoogleApiClient miGoogleApiClient = null;
private LocationRequest miLocationRequest = null;
private LocationRequest currLocation = null;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate MY SERVICE");
startGPS();
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG, "onDestroy");
stopGPS();
}
public boolean startGPS(){
Log.d(TAG, "startGPS");
try{
miGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(miConnectionCallbacks)
.addOnConnectionFailedListener(miOnConnectionFailedListener)
.build();
miGoogleApiClient.connect();
return true;
}catch(Exception e){
Log.d(TAG, "can't start GPS:"+e);
return false;
}finally{return false;}
}
public void stopGPS(){
Log.d(TAG, "stopGPS");
miGoogleApiClient.disconnect();
}
private LocationListener miLocationListener = new LocationListener(){
@Override public void onLocationChanged(Location location){
Log.d(TAG, "onLocationChanged:"+new Date(location.getTime())+", "+ location);
//...more work
currLocation = location;
}
};
private GoogleApiClient.ConnectionCallbacks miConnectionCallbacks = new GoogleApiClient.ConnectionCallbacks(){
@Override public void onConnected(Bundle bundle){
Log.d(TAG, "onConnected:"+bundle);
currLocation = LocationServices.FusedLocationApi.getLastLocation(miGoogleApiClient); //may be old
miLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
//.setSmallestDisplacement(10) //Won't use this for now
.setInterval(30000)
.setFastestInterval(30000);
LocationServices.FusedLocationApi.requestLocationUpdates(
miGoogleApiClient, miLocationRequest, miLocationListener);
}
@Override public void onConnectionSuspended(int i){
Log.d(TAG, "onConnectionSuspended:"+i);
}
};
private GoogleApiClient.OnConnectionFailedListener miOnConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener(){
@Override public void onConnectionFailed(ConnectionResult connectionResult){
Log.d(TAG, "onConnectionFailed:"+connectionResult);
}
};
}
我知道這種做法水渠電池快,但這個應用程序是爲特定的客戶端,而不是爲普通大衆所以沒關係。
¿是否應該發生這種行爲,或者手機有問題或者我的代碼有問題?我發現特別奇怪的是,即使谷歌地圖無法獲取位置,所以我想知道我的應用程序是否搞亂了手機本身。
編輯。
我必須重新啓動手機,因爲在Android設置菜單中關閉和打開GPS沒有任何區別。 (我的服務日誌顯示,如果我停止並重新啓動GPS,則無錯誤地啓動GPS)
雖然它應該做同樣的事情,你可以嘗試使用https://github.com/mcharmas/Android-ReactiveLocation在任何情況下,它可以節省很多行代碼 – cYrixmorten 2015-02-09 21:44:31
@cYrixmorten謝謝。這是一個有趣的圖書館。如您所說,它使用相同的代碼來處理GoogleApiClient,但樣板代碼可能是一個因素。我想知道在服務上下文中使用GoogleApi的注意事項。我會嘗試用你的建議做一個測試項目。 – user2997817 2015-02-10 23:06:04
除了記得在我知道的'onDestroy()'中取消訂閱外。我自己在服務中使用它。只需將另一個庫添加到混合中,然後使用EventBus https://github.com/greenrobot/EventBus在新位置可用於更新UI時進行廣播。這種工作方式與常規廣播幾乎相同,但不限於可序列化的數據,但適用於任何複雜的對象,並且比設置廣播接收器等代碼少。 – cYrixmorten 2015-02-11 08:47:35