我正在構建一個使用gps來定位用戶的android應用程序。我有一個名爲LocationService的類,它擴展了Service並實現了LocationListener。在我的活動中,我有以下代碼。當我想打開服務並偵聽位置時,我調用startLocationService()方法。所有這些工作正常。使用標籤時Android位置服務被破壞
然後我把這個活動放在tabhost的tab裏。現在,當我調用startLocationService()時,什麼也沒有發生,甚至不調用LocationService中的onCreate()方法。我檢查了該行中的「this」引用 Intent i = new Intent(this,LocationService.class); 它指的是正確的活動,而不是TabActivity。
我無法解釋爲什麼LocationService未啓動。我會很感激任何人都可以解決這個問題。
感謝, 保羅
private LocationService service = null;
private ServiceConnection svcConn = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder binder) {
service = (LocationService)binder;
try {
service.registerCallback(cbListener);
service.enableProximityPoints();
} catch (Throwable t) {
Log.e("MyPath", "Exception in call to registerAccount()", t);
}
}
public void onServiceDisconnected(ComponentName className) {
service = null;
}
};
private void startLocationService(){
Intent i = new Intent(this, LocationService.class);
bindService(i, svcConn, 0);
startService(i);
}
這解決了我的問題,非常感謝。 – Paul 2011-03-30 15:34:47
只是注意到我沒有選擇這個作爲正確的答案,現在會這樣做。 – Paul 2014-02-23 06:24:39