ServiceConnection :: onServiceConnected不叫尽管语境:: bindService返回true?语境、不叫、onServiceConnected、ServiceCo

2023-09-06 06:40:01 作者:扰人清梦

我一直在试图绑定已启动对启动从活动的服务。启动开机的code的大多是从的即时通讯。

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

 <! - 接收器 - ><接收机器人:名字=。receiver.LifestylePPAutoStarter机器人:工艺=android.process.lifestylepp>&所述;意图滤光器><作用机器人:名字=android.intent.action.BOOT_COMPLETED/>&所述; /意图滤光器>< /接收器><! - 服务 - ><服务机器人:名字=。service.LifestylePPService机器人:工艺=android.process.lifestylepp机器人:启用=真机器人:出口=真正的>&所述;意图滤光器><作用机器人:名字=edu.gatech.lifestylepp.ILifestylePPService/><作用机器人:名字=edu.gatech.lifestylepp.SERVICE/>&所述; /意图滤光器>< /服务>&所述;! - 活动 - ><活动机器人:名字=。app.LifestylePPActivity机器人:标签=@字符串/ APP_NAME>&所述;意图滤光器><作用机器人:名字=android.intent.action.MAIN/><类机器人:名字=android.intent.category.LAUNCHER/>&所述; /意图滤光器>< /活性GT; 

接收器开始在启动该服务没有任何问题。然而,当我尝试将服务从我的活动绑定,上下文:: bindService返回true,但ServiceConnection :: onServiceConnected不会被调用。此外,当我开始从它按预期工作活动服务(ServiceConnection :: onServiceConnected被调用)。

解决方案   

此外,当我开始从服务  它按预期工作活动  (ServiceConnection :: onServiceConnected  被调用)。

startService()不涉及 ServiceConnection 对象。

摆脱这两个的android:过程=android.process.lifestylepp从你的清单线。这可能是你的困难之源,更重要的是它是不太可能你真的需要两个过程,所有的开销需要。

详解ServiceConnection

I've been trying to bind a service that was started on boot from an activity. The code for starting on boot was mostly taken from the instant messenger.

This is the AndroidManifest.xml definition for the 3 main components:

	<!-- 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>

The receiver starts the service on boot without any problems. However when I try to bind the service from my activity, Context::bindService returns true, but ServiceConnection::onServiceConnected is never called. Also, when I start the service from the activity it works as expected (ServiceConnection::onServiceConnected is called).

解决方案

Also, when I start the service from the activity it works as expected (ServiceConnection::onServiceConnected is called).

startService() does not involve a ServiceConnection object.

Get rid of both the android:process="android.process.lifestylepp" lines from your manifest. That may be the source of your difficulty, and more importantly it is very unlikely you really need two processes and all the overhead that requires.