ServiceConnection::onServiceConnected not called e

2019-02-18 04:38发布

问题:

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).

回答1:

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.