gpt4 book ai didi

android - 绑定(bind)远程服务 (AIDL)

转载 作者:行者123 更新时间:2023-12-04 15:06:18 25 4
gpt4 key购买 nike

互联网上有许多关于 StackOverflow 和指南的问题,包括 Android 自己的关于如何让这个特定功能正常工作的文档。

我相信我已严格遵守所有说明和细微差别,但这里有一些我用来尝试解决问题的引用问题:

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

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

无法启动服务 Intent { cmp=com.example.dumpsterfire/.DumpsterService } U=0: 未找到

下面是两个app的相关配置

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(客户端/服务绑定(bind)器)

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. 我也试过:

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

最佳答案

如果您在 Android 11 上对此进行测试,您的问题可能是您的客户端应用无法看到服务应用。获取this sample service client寻找this sample service在 Android 11 上,我需要添加一个 <queries>元素添加到我客户的 list 中,包含服务应用程序的包:

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

关于android - 绑定(bind)远程服务 (AIDL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66017964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com