服务不会通过bindservice使用bind_auto_create开始

发布于 2025-02-13 03:32:10 字数 2248 浏览 1 评论 0原文

我有一个带有三个架子的项目:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2025-02-20 03:32:10

bindservice() Android 11+上的。带有服务的应用程序不需要更改,而是与客户端的应用程序 - 将调用bindservice()的代码 - 将需要&lt; queries&gt; element清单,通过服务识别应用程序的应用ID。

    <queries>
        <package android:name="com.example.server" />
    </queries>

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 call bindService()will need a <queries> element in its manifest, identifying the application ID of the app with the service.

    <queries>
        <package android:name="com.example.server" />
    </queries>
东北女汉子 2025-02-20 03:32:10

通过将&lt; intent&gt;添加到服务应用程序清单

<application ...>
    <service android:name=".RemoteService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.server.RemoteService"/>
        </intent-filter>
    </service>
</application>

&lt; queries&gt;&gt; intent&gt;

<queries>
    <intent>
        <action android:name="com.example.server.RemoteService" />
    </intent>
</queries>

<application
    ...>
</application>

最终, 服务器模块应在没有任何导出标志的情况下可见。

感谢@commonsware的&lt; queries&gt;提示。

Have finally made it work by adding <intent> to the service app manifest

<application ...>
    <service android:name=".RemoteService"
        android:exported="true">
        <intent-filter>
            <action android:name="com.example.server.RemoteService"/>
        </intent-filter>
    </service>
</application>

AND <queries><intent> to the client app manifest

<queries>
    <intent>
        <action android:name="com.example.server.RemoteService" />
    </intent>
</queries>

<application
    ...>
</application>

But 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文