我正在构建一个使用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