第2步:生成ExampleServer可执行程序
首先在frame/base目录下新建一个ExampleServer目录,用来保存ExampleServer可执行程序的源代码文件:
- $cd ~/WORKING_DIRECTORY/framework/base/
- $mkdir ExampleServer
- $cd ExampleServer
其中ExampleServer.cpp文件的内容如下:
- // File: ExampleServer.cpp
- #include <sys/types.h>
- #include <unistd.h>
- #include <grp.h>
- #include <binder/IPCThreadState.h>
- #include <binder/ProcessState.h>
- #include <binder/IServiceManager.h>
- #include <utils/Log.h>
- #include <private/android_filesystem_config.h>
- #include "../ExampleService/ExampleService.h"
- using namespace android;
- int main(int argc, char** argv)
- {
- sp<ProcessState> proc(ProcessState::self()); // 要想使用Binder机制,必须要创建一个ProcessState对象
- sp<IServiceManager> sm = defaultServiceManager();
- LOGI("ServiceManager: %p", sm.get());
- ExampleService::instantiate();
- ProcessState::self()->startThreadPool();
- IPCThreadState::self()->joinThreadPool();
- return 0;
- }
- # File: Android.mk
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_SRC_FILES:= \
- ExampleServer.cpp
- LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
- LOCAL_SHARED_LIBRARIES := \
- libutils libbinder libExample
- LOCAL_MODULE_TAGS := optional
- LOCAL_PRELINK_MODULE := false
- LOCAL_MODULE := ExampleServer
- include $(BUILD_EXECUTABLE)
- $cd ~/WORKING_DIRECTORY/
- $mmm framework/base/ExampleServer/

这样就编译完了可执行程序ExampleServer了.可以在out/target/product/generic/system/bin/目录下看到可执行程序ExampleServer:

OK,就这样binder的服务器端程序就完成了。
有关如何完成binder客户端的介绍请看 http://www.linuxidc.com/Linux/2012-01/50767.htm
