我一直在试图绑定从活动启动时启动的服务。启动时启动的代码主要取自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)。
我知道startService()不涉及ServiceConnection。我一直在说的是:当我从活动开始服务时,我可以使用bindService(intent,connection,0)绑定它,没有任何问题。只有从服务器启动服务时才会出现问题。不过谢谢你的建议!不幸的是,我已经尝试在同一个进程上运行活动和服务,并没有改变任何东西。 – kloffy 2009-10-18 19:00:16
在bindService()调用中添加BIND_AUTO_CREATE标志。 – CommonsWare 2009-10-18 19:32:30
这不会导致新的服务启动吗?该服务已在运行,它由接收器启动时创建。我只是想从活动中绑定它。 – kloffy 2009-10-19 17:39:23