使用Android原生Binder发送自定义对象

26

我只能找到关于Java的Parcelable示例。我的目标是创建一个简单的服务和客户端,使用Binder接收和发送序列化的自定义对象,而且这是使用本地C++(而不是NDK)。

MyClass
+ std::string
+ enum
+ int
+ bool

1
没有NDK会有什么问题吗?可以使用JNI吗? - vgonisanz
似乎 Parcelable.h 接口可在 Android 6.0 及以上版本使用,而我正在使用 5.1。 - Cumatru
我认为在Android上没有NDK的情况下无法让C++代码运行。这是用于使代码工作的工具链/交叉编译器。它还包括(几乎总是需要的)STL。 - Arne Fischer
1个回答

1

由于您正在使用Android 5.1,因此我将坚持Lollipop可用的内容。有更好的工具,如aidl-cpp,可以完成此操作,从而节省了手动编写样板代码的麻烦。

首先,在IFooService.h中创建服务的接口。

class IFooService: public IInterface {
    public:
        DECLARE_META_INTERFACE(FooService);
        virtual int myFunc(const String8& str, int enumParam, int intParam, int boolParam) = 0;

};

然后编写服务的编组代码,根据问题要求实现一个函数。
class BnFooService: public BnInterface<IFooService>
{
public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

enum {
    MY_FUNC = IBinder::FIRST_CALL_TRANSACTION,
};

/* BinderProxy code, for clients of the service to call into the service and have
   the paremeters marshalled properly across the Binder interface. Executes in the
   client process context
 
*/
class BpFooService: public BpInterface<IFooService> {
public:
    virtual int myFunc(const String8& str, int enumParam, int intParam, int boolParam) {
        //2 Parcel objects, 1 to write the data to and another to receive it in
        Parcel data, reply;
        //This serves as the token for the Binder framework for a sanity check
        data.writeInterfaceToken(IFooService::getInterfaceDescriptor());
        data.writeString8(str);
        data.writeInt32(enumParam);
        data.writeInt32(intParam);
        data.writeInt32(boolParam);
        return remote()->transact(MY_FUNC, data, &reply);
    }
}

IMPLEMENT_META_INTERFACE(MediaPlayerService, "com.somedomain.IFooService");

/* Actual Binder implementation of the service which unpacks the data from the
   incoming Parcel and handles the call accordingly     
*/
status_t BnFooService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
    switch (code) {
        case MY_FUNC: {
            //The same token is checked
            CHECK_INTERFACE(IFooService, data, reply);
            //Read off in exact same order as written to the data Parcel above
            String8 str = data.readString8();
            int enumParam = data.readInt32();
            int intParam = data.readInt32();
            int boolParam = data.readInt32();
            reply->writeInt32(myFunc(str, enumParam, intParam, boolParam));
            return NO_ERROR;
        }
        break;
    default:
        
}

然后创建一个名为main_foo.cpp的文件,并添加以下代码作为服务进程的入口点:
int main() {
    if(!FooService::publish()) { //Call into your code to register with ServiceManager
        ALOGE("Can't instantiate FooService! Exiting");
        return 1;
    }
    ProcessState::self()->startThreadPool(); //Required for the binder threads
    IPCThreadState::self()->joinThreadPool(); //Infinite loop that waits for inbound connections
}

最后,在FooService.cpp中实现服务接口。

bool FooService::publish() {
    defaultServiceManager()->addService(String16("foo_service"), new FooService());
    sp<IBinder> binder = defaultServiceManager()->checkService(String16("foo_service"));
    while(binder == NULL && /*some upper time limit*/){
        ALOGI("Waiting for foo_service");
    }
    return binder != NULL;
}

int FooService::myFunc(const String8& str, int enumParam, int intParam, int boolParam) {
    //business logic
    return 0;
}

请注意,这不是为普通应用程序开发人员准备的代码,而是为在Android框架中运行的原生服务(如InputFlinger、SurfaceFlinger等)准备的代码。此代码需要像Parcel.h、IInterface.h等框架头文件,这些文件在SDK中不可用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接