Cannot bind to exported Android service

2019-08-16 16:16发布

问题:

I have two Android apps, which I will call my "service app", and my "client app". My client app needs to bind to a service exported by my service app.

Unfortunately, even after starting my service app, I can't get my client app to recognize that the service exported by my service app even exists.

My service app's manifest looks like this:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service">
<application android:allowBackup="false" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="com.example.service.App" android:theme="@style/AppTheme">
    ...
    <service android:exported="true" android:name="com.example.service.MyService"/>

</application>
</manifest>

My service app's MyService class looks like this:

package com.example.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

public class MyService extends Service {
    private final Messenger messenger = new Messenger(new MyHandler());

    private static class MyHandler extends Handler {

        public void handleMessage(Message message) {
            //blah blah blah
        }
    }

    public IBinder onBind(Intent intent) {
        return this.messenger.getBinder();
    }
}

And my client app's service binding logic looks like this:

public class MainActivity extends AppCompatActivity {

    public static final String tag = "MyClientApp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final Intent intent = new Intent();
        intent.setClassName("com.example.service", "com.example.service.MyService");

        boolean success = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        Log.e(tag, "success=" + success); //always prints false!

        //I have also tried:
        //1. getApplicationContext().bindService(...) 
        //   (always returns false)
        //2. startService(intent);
        //   (always returns null)
        //3. getApplicationContext().startService(intent);
        //   (always returns null)
    }

    private Messenger mService;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            mService = new Messenger(iBinder);
            Log.e(tag, "SERVICE CONNECTED");
            //(method never called)
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(tag, "SERVICE DISCONNECTED");
            mService = null;
            //(method never called)
        }

        @Override
        public void onBindingDied(ComponentName name) {
            Log.e(tag, "BINDING DIED " + name);
            //(method never called)
        }
    };
}

When I launch:

adb shell am startservice -n "com.example.service/.MyService"

it gives me the following error message:

Starting service: Intent { cmp=com.example.service/.MyService launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } }
Error: Not found; no service started.

Help on this matter would be greatly appreciated. For reference, I am running both apps on my Samsung Galaxy S8 phone.

As requested by @pskink, here is the full manifest file from the service app's apk:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="2018003"
    android:versionName="2018.003"
    package="com.example.service">

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="27" />

    <uses-permission
        android:name="android.permission.VIBRATE" />

    <application
        android:theme="@ref/0x7f0c0006"
        android:label="@ref/0x7f0b001d"
        android:icon="@ref/0x7f070061"
        android:name="com.example.service.App"
        android:allowBackup="false">

        <activity
            android:label="@ref/0x7f0b001d"
            android:name="com.example.service.BlipActivity"
            android:launchMode="2"
            android:configChanges="0x5a0"
            android:windowSoftInputMode="0x13">

            <intent-filter>

                <action
                    android:name="android.intent.action.MAIN" />

                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:label="@ref/0x7f0b001d"
            android:name="com.example.service.GameActivity"
            android:exported="true"
            android:launchMode="2"
            android:configChanges="0x5a0"
            android:windowSoftInputMode="0x13" />

        <service
            android:name="com.example.service.MyService"
            android:exported="true" />
    </application>
</manifest>