2011-03-30 43 views
0

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

回答

1

看起来像其他人遇到过类似的问题:http://www.mail-archive.com/[email protected]/msg08047.html

他们用了移动服务连接到TabActivity类解决他们的问题。

+0

这解决了我的问题,非常感谢。 – Paul 2011-03-30 15:34:47

+0

只是注意到我没有选择这个作为正确的答案,现在会这样做。 – Paul 2014-02-23 06:24:39

0

我检查在 线意图 “这个” 引用i =新意图(此, LocationService.class);它将 指向正确的活动,而不是 TabActivity。

这可能是问题所在 - 我发现活动作为标签的内容行为有点不同于我期望的有时。

在这是 '选项卡内容' 活动试图定义一个类实例成员的活动......

然后在onCreate() ...

parent = this.getParent(); // Gets the TabActivity context 

然后在startLocationService()使用..

Intent i = new Intent(parent, LocationService.class); 

不知道它是否会工作,但值得一试。我目前正在开发一个基于TabActivity的应用程序,并且我不得不在不同的地方在'tab content'活动中做这样的事情来让事情正常工作。

+0

感谢您的回复,我必须将所有ServiceConnection代码移动到TabActivity,然后才能正常工作。 (查看其他回复) – Paul 2011-03-30 15:39:07

+0

@Paul:感谢您的确认(我的意思是它没有工作)。值得注意的是未来。 – Squonk 2011-03-30 15:51:19