绑定远程服务 (AIDL)

时间:2021-02-02 21:33:55

标签: android

关于 StackOverflow 的问题和互联网上的指南有很多,包括 Android 自己的关于使此特定功能正常工作的文档。

我相信我已经遵循了这封信的所有说明和细微差别,但这里有一些我用来尝试解决问题的参考问题:

AIDL interface between two applications
Service not binding
Android: Binding to a remote service

我遇到的问题是无法定位服务本身。无论是否已经运行,我都遇到以下错误:

Unable to start service Intent { cmp=com.example.dumpsterfire/.DumpsterService } U=0: not found

这里是两个应用的相关配置

DumpsterFire(服务器/服务源)

build.gradle.kts

plugins {
    id("com.android.application")
    id("kotlin-android")
}

android {
    compileSdkVersion(30)
    buildToolsVersion = "30.0.3"

    defaultConfig {
        applicationId = "com.example.dumpsterfire"
        minSdkVersion(24)
        targetSdkVersion(30)
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation(fileTree("/libs") { include("*.jar", "*.aar") })

    implementation("androidx.core:core-ktx:1.3.2")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.2.1")
    implementation("androidx.constraintlayout:constraintlayout:2.0.4")
    testImplementation("junit:junit:4.13.1")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}

AndroidManifest

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DumpsterFire">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".DumpsterService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>

DumpsterService.kt

class DumpsterService : Service() {
    override fun onBind(intent: Intent?): IBinder {
        return object : IDemoAidl.Stub() {
            override fun printAcross(message: String?) {
                Log.d("chaser", "Hello, I've been instructed to say:\n$message")
            }
        }
    }
}

IDemoAidl.aidl

interface IDemoAidl {
    void printAcross(String message);
}

ClientDumpster(客户端/服务绑定器)

MainActivity.kt

//...
override fun onCreate(savedInstanceState: Bundle?) {
    //...
    bindService(
            Intent().setClassName("com.example.dumpsterfire", ".DumpsterService"),
            connection,
            BIND_AUTO_CREATE
    )
}

val connection = object : ServiceConnection {
    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        IDemoAidl.Stub.asInterface(service)
                .printAcross("Assuming direct control")
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        Log.d("chaser", "host service terminated")
    }
}
//...

IDemoAidl.aidl

interface IDemoAidl {
    void printAcross(String message);
}

P.S. 我试图让有问题的服务在后台运行,并通过 adb shell 命令 dumpsys activity services 验证它的存在。它不会改变我收到的错误。

P.P.S. 我也试过:

  • 从头开始重新安装这两个应用。
  • 进行干净的构建。
  • 使缓存无效/重新启动。

1 个答案:

答案 0 :(得分:2)

如果您在 Android 11 上对此进行测试,您的问题可能是您的客户端应用无法看到服务应用。为了让 this sample service client 在 Android 11 上找到 this sample service,我需要将 <queries> 元素添加到我客户端的清单中,以及服务应用程序的包:

<queries>
  <package android:name="com.commonsware.android.r.embed.server" />
</queries>