我無法讓GPS的onLocationChanged在不同的線程上運行。我知道如何在我調用一個函數但使用GPS時管理UI線程,但我不主動調用該函數。Android GPS回調關閉UI線程
我的意圖是每次GPS接收到讀數時都會有閃光。我已將此函數放入Runnable中。我將此函數傳遞給實現LocationListener的類。然後在主類中,我開始了一個調用requestLocationUpdates的新線程。我希望LocationListener的onLocationChanged能夠在不同的線程中運行,併發布到回調函數中,並在UI線程中創建必要的UI效果。不幸的是,每次嘗試調用requestLocationUpdates時程序崩潰。什麼是正確的做法?
現在它看起來像這樣
主要類:
final Runnable changeLight = new Runnable(){
public void run(){
// do stuff
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capture);
status = (ImageView) findViewById(R.id.status);
database = new DatabaseManager(this);
new Thread(){
public void run(){
location = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new GPSManager(database, changeLight, light, status);
location.requestLocationUpdates("gps", 10000L, 0, listener);
}
}.start();
}
LocationListener的類:
public void onLocationChanged(Location location) {
if (location.getAccuracy() <= 32.0){
light = lightColors.Green;
status.post(callback);
database.insertData(location);
}
else{
light = lightColors.Yellow;
status.post(callback);
}
}
異常說無法創建內螺紋已不叫尺蠖處理程序.prepare()
我添加了代碼和異常 – xster 2010-07-06 16:52:27