2013-10-09 56 views
0

我有一個關於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(); 
} 
} 
+0

看起來很奇怪......當你說「後臺服務」你到底用了什麼? –

+0

更新了我的答案 –

+0

問題是,它不是應用程序崩潰...整個電話塊,直到重新啓動 –

回答

0

服務在主線程中運行!!!!這就是爲什麼它凍結UI。

注意:服務在其宿主進程的主線程中運行 - 服務不會創建自己的線程,也不會在單獨的進程中運行(除非另有指定)。這意味着,如果您的服務將執行任何CPU密集型工作或阻止操作(如MP3播放或網絡連接),則應在服務中創建一個新線程來完成此項工作。通過使用單獨的線程,您將降低應用程序無響應(ANR)錯誤的風險,並且應用程序的主線程可以保持專用於用戶與您的活動進行交互。

http://developer.android.com/guide/components/services.html

您應該由IntentService做到這一點,例如。或者在該服務上添加處理程序/線程。

+0

這個死亡沒有回答爲什麼整個手機阻止。 – AlexWien

+0

我會嘗試一下,看看有什麼會改變 –

相關問題