有许多关于StackOverflow和网络指南的问题,包括Android有关此特定功能的官方文档。
我相信我已经逐字遵循了所有的指示和细微差别,但这是一些参考问题,我用来尝试解决我的问题:
两个应用程序之间的AIDL接口
服务未绑定
Android: 绑定到远程服务
我遇到的问题是找不到Service本身。无论它是否正在运行,我都会遇到以下错误:
无法启动Intent { cmp=com.example.dumpsterfire/.DumpsterService } U=0的服务:未找到
这是两个应用程序的相关配置
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);
}
客户端废纸篓 (Client/Service binder)
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. 我也尝试过以下方法:
- 从头重新安装两个应用程序。
- 进行清洁构建。
- 清除缓存/重启。
DumpsterFire
应用程序有一个活动,并且在DumpsterClient
安装/启动之前已经被安装/启动过了。 - nukeforumbuild.gradle
文件。明确一下,我已经从DumpsterFire
中显式运行了DumpsterService
并使用dumpsys activity services | grep com.example
实际验证了其存在。输出服务包与我在此处详细说明的包是一致的。 - nukeforumDumpsterFire
项目中的 AndroidManifest,并添加了其构建脚本的完整内容。我注意到在帖子中展示的包名中出现了一个拼写错误,我已在项目和这里进行了更正。然而,更正这个错误并没有解决问题。 - nukeforum