java - 如何连接到 java 中的 Binder C++ 服务?

标签 java c++ android

我想在 C++ 中创建一个 Binder 服务并能够绑定(bind)到它 来自java的服务。我的问题是如何?我模仿了 在 frameworks/base/camera/tests/CameraServiceTest/中实现 相机服务测试.cpp。它编译得很好,没有任何警告,我 可以运行它。但是我如何连接到它呢?

服务代码在这里:

/* Imitating frameworks/base/camera/tests/CameraServiceTest/
CameraServiceTest.cpp */

#include <binder/IInterface.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/IBinder.h>

using namespace android;


class IPokeService : public IInterface {
        protected:
                enum {
                        POKE = IBinder::FIRST_CALL_TRANSACTION
                };

        public:
                DECLARE_META_INTERFACE(PokeService);
                virtual void poke() = 0;
};

class BnPokeService : public BnInterface<IPokeService> {
        virtual status_t onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags = 0);
};

class BpPokeService : public BpInterface<IPokeService> {
        public:
                BpPokeService(const sp<IBinder>& impl) :
BpInterface<IPokeService>(impl) {
                }

                virtual void poke() {
                        Parcel data, reply;
                        remote()->transact(POKE, data, &reply);
                }
};

IMPLEMENT_META_INTERFACE(PokeService, "PokeService");

status_t BnPokeService::onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags) {
        switch(code) {
                case POKE: {
                          poke();
                          return NO_ERROR;
                } break;
                default:
                        return BBinder::onTransact(code, data, reply, flags);
        }
}

class PokeService : public BnPokeService {
        virtual void poke() {
                printf("Poked\n");
        }
};

int main(int argc, char **argv)
{
        defaultServiceManager()->addService(String16("PokeService"), new
PokeService());
        ProcessState::self()->startThreadPool();

        android::ProcessState::self()->startThreadPool();
        LOGI("Poke service is now ready");
        IPCThreadState::self()->joinThreadPool();

        return 0;
}

这是我的 Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := main.cpp

LOCAL_CFLAGS += -DPLATFORM_ANDROID

LOCAL_MODULE := poke

# for now, until I do a full rebuild.
LOCAL_PRELINK_MODULE := false

LOCAL_SHARED_LIBRARIES += liblog
LOCAL_SHARED_LIBRARIES += libutils libui libcutils
LOCAL_SHARED_LIBRARIES += libbinder

include $(BUILD_EXECUTABLE)

当我在 java 中列出时,该服务不会显示:

Context context = getBaseContext();
ActivityManager am = (ActivityManager)
context.getSystemService(Activity.ACTIVITY_SERVICE);

List<RunningServiceInfo> serviceInfos = am.getRunningServices(50);

for (RunningServiceInfo service : serviceInfos) {
        Log.i("Service", "Process " + service.process + " with component " + service.service.getClassName());
}

那只会从 logcat 给我:

I/Service (  715): Process system with component
com.android.internal.service.wallpaper.ImageWallpaper
I/Service (  715): Process com.android.phone with component
com.android.phone.BluetoothHeadsetService
I/Service (  715): Process com.android.inputmethod.latin with
component com.android.inputmethod.latin.LatinIME

如有任何帮助,我们将不胜感激!

亲切的问候, 塞缪尔

最佳答案

我的理解是你不能,因为没有为客户端应用程序提供任何方法来为你获取目标句柄。

如果您正在编写特权 native 服务,您可以使用您选择的名称向 Binder 驱动程序注册,但大概您不是拥有设备系统证书的 oem。

典型的 java 类型服务会导致句柄生成基于 Intent 而不是 Binder 服务名称,但是您必须对 Binder 的现有接口(interface)做很多不鼓励的伪造,以便能够从 native 处理它代码而不是来自 java。

如果您不想通过 jni 返回并从 java 返回,最好使用 unix 域套接字而不是 Binder 。

(很抱歉,如果我使用了错误的术语 - 我在一个月前非常详细地研究了这个,发现它不能做我想做的事并放弃了它)

关于java - 如何连接到 java 中的 Binder C++ 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978869/

相关文章:

c++ - 使用 GCC 和手动 makefile 进行增量构建?

java - 线程池中的多个线程在同一个List中写入数据

java - JComboBox 中的 List<String> 数组

java - 使用纯 Java 的 AuthenticationException LDAP

c++ - 检查双 "d < 0"可以吗?

c++ - GMP 中变量的初始化

android - 如何在android中的Clicking事件中将ListView选择模式从单个切换到多个?

android - 单击按钮后,从recyclerview中删除项目-Kotlin MVVM Firestore

安卓。 TranslateAnimation 无效

java - GUI 应用程序中线程的最佳方法(Java、swing)