我的好奇的問題是,在我的應用程序中,GPS定位需要相當長的時間(因爲它傾向於這樣做),因此我想將它作爲自己的線程運行,而用戶在其他視圖中進行選擇。在Android的後臺線程中運行GPS定位
我的結構是,我有一個「主」視圖,它始終在後臺處於活動狀態,然後向用戶顯示一系列他或她做出幾個選擇的視圖。最後,根據所作出的選擇和當前位置向用戶呈現結果。所有這些工作,但我想要將GPS位移動到它自己的線程,以便用戶不必等待它完成。
所有用戶的選擇和GPS座標存儲在一個名爲CurrentLogEntry的單例中。程序不同部分之間的所有通信都是通過它執行的。
我創建在主視圖中的handleMessage(Message msg)
覆蓋,然後我實現在Main.java此功能:
void startGPSThread() {
Thread t = new Thread() {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean isDebug = CurrentLogEntry.getInstance().isDebug();
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status changes */
String tag = "onStatusChanged, ";
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.w(tag, "Status Changed: Out of Service");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.w(tag, "Status Changed: Temporarily Unavailable");
break;
case LocationProvider.AVAILABLE:
break;
}
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
// This is called if the GPS is disabled in settings.
// Bring up the GPS settings
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
public void onLocationChanged(Location location) {
// Once a location has been received, ignore all other position
// updates.
locationManager.removeUpdates(locationListener);
locationManager.removeUpdates(this);
// Make sure that the received location is a valid one,
// otherwise show a warning toast and hit "back".
if (location == null) {
String warningString = "Location was unititialized!";
if (isDebug) {
Toast.makeText(getApplicationContext(),
warningString,
Toast.LENGTH_LONG).show();
}
KeyEvent kev = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
onKeyDown(KeyEvent.KEYCODE_BACK, kev);
}
CurrentLogEntry.getInstance().setUserLatitude(location.getLatitude());
CurrentLogEntry.getInstance().setUserLongitude(location.getLongitude());
//Send update to the main thread
int result = 0;
if (location.getLatitude() == 0 || location.getLongitude() == 0) {
result = -1;
}
messageHandler.sendMessage(Message.obtain(messageHandler, result));
}
};
@Override
public void run() {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
// Wait until a position has bee acquired.
while(!CurrentLogEntry.getInstance().isReadyToCalculate()){
try {
wait(250);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
t.start();
}
調用該函數在Main.onCreate()
。
不幸的是,這根本不起作用。該程序崩潰,只要它達到locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
,這讓我完全陷入困境。
任何人都可以啓發我爲什麼這不會作爲後臺線程運行?另外,我最終是否真的需要等待輪詢來確保線程保持活動狀態直到它接收到它的數據?在我想把它放在自己的線程中之前,GPS位工作得很好,那麼我在做什麼錯了?
你可以發佈錯誤日誌嗎? – PedroAGSantos
我很想去,但沒有。我得到的只是「應用程序意外停止,請重試。」 Netbeans不給我任何輸出。編輯:是否有其他輸出,我錯過了? –
Netbeans調試器控制檯僅包含有關我的斷點和「用戶程序正在運行 用戶程序已完成」的消息。 –