關于StackOverflow和互聯網上的指南有很多問題,包括Android自己關于如何讓這個特定功能工作的文檔。
我相信我已經遵循了所有的指示和細節,但這里有一些參考問題,我曾試圖解決我的問題:
兩個應用程序之間的AIDL接口服務未綁定Android:綁定到遠程服務
我遇到的問題是無法定位服務本身。無論它是否已經運行,我都會遇到以下錯誤:
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);
}
客戶端轉儲程序(客戶端/服務綁定器)
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、 我試圖讓這個服務已經在后臺運行,并通過adb shell命令dumpsys activity services
驗證它的存在。它不會改變我收到的錯誤。
P、 另外,我也試過:
- 從頭開始重新安裝這兩個應用程序。
- 進行干凈的構建。
- Invalidate caches/Restart.
如果你在android11上測試這個,你的問題可能是你的客戶端程序看不到服務程序。為了讓這個示例服務客戶機在Android 11上找到這個示例服務,我需要在我的客戶機清單中添加一個
<queries>
元素,以及服務應用程序包: