2009-10-17 79 views
4

我一直在试图绑定从活动启动时启动的服务。启动时启动的代码主要取自instant messenger即使Context :: bindService返回true,ServiceConnection :: onServiceConnected仍未调用?

这是3个主要组件AndroidManifest.xml中定义:

<!-- Receiver --> 
    <receiver android:name=".receiver.LifestylePPAutoStarter" 
     android:process="android.process.lifestylepp"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"/> 
     </intent-filter> 
    </receiver> 

    <!-- Service --> 
    <service android:name=".service.LifestylePPService" 
     android:process="android.process.lifestylepp" 
     android:enabled="true" 
     android:exported="true"> 
     <intent-filter> 
      <action android:name="edu.gatech.lifestylepp.ILifestylePPService" /> 
      <action android:name="edu.gatech.lifestylepp.SERVICE" /> 
     </intent-filter> 
    </service> 

    <!-- Activity --> 
    <activity android:name=".app.LifestylePPActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

接收器开始在启动该服务没有任何问题。但是,当我尝试绑定来自我的活动的服务时,Context :: bindService返回true,但从不调用ServiceConnection :: onServiceConnected。另外,当我从活动中启动服务时,它按预期工作(调用ServiceConnection :: onServiceConnected)。

回答

2

此外,当我从 启动该服务能够正常运行 活动(ServiceConnection :: onServiceConnected 被调用)。

startService()不涉及ServiceConnection对象。

摆脱清单中的android:process="android.process.lifestylepp"行。这可能是你困难的根源,更重要的是,你真的不太需要两个进程和所有需要的开销。

+0

我知道startService()不涉及ServiceConnection。我一直在说的是:当我从活动开始服务时,我可以使用bindService(intent,connection,0)绑定它,没有任何问题。只有从服务器启动服务时才会出现问题。不过谢谢你的建议!不幸的是,我已经尝试在同一个进程上运行活动和服务,并没有改变任何东西。 – kloffy 2009-10-18 19:00:16

+1

在bindService()调用中添加BIND_AUTO_CREATE标志。 – CommonsWare 2009-10-18 19:32:30

+0

这不会导致新的服务启动吗?该服务已在运行,它由接收器启动时创建。我只是想从活动中绑定它。 – kloffy 2009-10-19 17:39:23

相关问题