服务不会通过bindservice使用bind_auto_create开始
我有一个带有三个架子的项目:
com.example.library - 定义AIDL接口
// IRemoteService.aidl
package com.example.library;
interface IRemoteService {
int add(int a,int b);
int sub(int a,int b);
}
com.example.server.server - 实现服务,取决于库
// AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...
package="com.example.server">
<application
...>
<service android:name=".RemoteService"/>
</application>
</manifest>
// RemoteService.kt
class RemoteService: Service() {
override fun onBind(p0: Intent?): IBinder {
return object : IRemoteService.Stub() {
@Throws(RemoteException::class)
override fun add(a: Int, b: Int): Int {
return a + b
}
@Throws(RemoteException::class)
override fun sub(a: Int, b: Int): Int {
return a - b
}
}
}
}
com 。
// MainActivity.kt
class MainActivity : AppCompatActivity(), ServiceConnection {
companion object {
const val TAG = "MainActivity"
}
private var service: IRemoteService? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
service?.let {
Log.i(TAG, it.add(10, 20).toString())
}
}
}
override fun onServiceConnected(componentName: ComponentName?, binder: IBinder?) {
Log.i(TAG, "onServiceConnected")
service = IRemoteService.Stub.asInterface(binder)
}
override fun onServiceDisconnected(componentName: ComponentName?) {
Log.i(TAG, "onServiceDisconnected")
service = null
}
override fun onStart() {
super.onStart()
bindService(Intent("com.example.server.RemoteService")
.setPackage("com.example.server"), this, BIND_AUTO_CREATE)
}
override fun onStop() {
unbindService(this)
super.onStop()
}
}
有什么想法,可能是什么原因?
I have a project with three mudules:
com.example.library - defines the AIDL interface
// IRemoteService.aidl
package com.example.library;
interface IRemoteService {
int add(int a,int b);
int sub(int a,int b);
}
com.example.server - implements the service, depends on the library
// AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...
package="com.example.server">
<application
...>
<service android:name=".RemoteService"/>
</application>
</manifest>
// RemoteService.kt
class RemoteService: Service() {
override fun onBind(p0: Intent?): IBinder {
return object : IRemoteService.Stub() {
@Throws(RemoteException::class)
override fun add(a: Int, b: Int): Int {
return a + b
}
@Throws(RemoteException::class)
override fun sub(a: Int, b: Int): Int {
return a - b
}
}
}
}
com.example.client - consumes the service, depends on the library
// MainActivity.kt
class MainActivity : AppCompatActivity(), ServiceConnection {
companion object {
const val TAG = "MainActivity"
}
private var service: IRemoteService? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.button).setOnClickListener {
service?.let {
Log.i(TAG, it.add(10, 20).toString())
}
}
}
override fun onServiceConnected(componentName: ComponentName?, binder: IBinder?) {
Log.i(TAG, "onServiceConnected")
service = IRemoteService.Stub.asInterface(binder)
}
override fun onServiceDisconnected(componentName: ComponentName?) {
Log.i(TAG, "onServiceDisconnected")
service = null
}
override fun onStart() {
super.onStart()
bindService(Intent("com.example.server.RemoteService")
.setPackage("com.example.server"), this, BIND_AUTO_CREATE)
}
override fun onStop() {
unbindService(this)
super.onStop()
}
}
The problem is that onServiceConnected
is never triggered, which means the service never gets started.
Any ideas please what could be the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bindservice()
Android 11+上的。带有服务的应用程序不需要更改,而是与客户端的应用程序 - 将调用bindservice()
的代码 - 将需要&lt; queries&gt;
element清单,通过服务识别应用程序的应用ID。bindService()
on Android 11+ is governed by package visibility rules. Your app with the service does not need changes, but the app with the client — the code that will callbindService()
— will need a<queries>
element in its manifest, identifying the application ID of the app with the service.通过将
&lt; intent&gt;
添加到服务应用程序清单和
&lt; queries&gt;&gt; intent&gt;
最终, 服务器模块应在没有任何导出标志的情况下可见。
感谢@commonsware的
&lt; queries&gt;
提示。Have finally made it work by adding
<intent>
to the service app manifestAND
<queries><intent>
to the client app manifestBut that is quite weird because the server module should be visible to the client without any exported flag.
Thanks to @CommonsWare for the
<queries>
hint.