首先定义一个模块的android.mk和.rc文件,这里我的问题是。我只能手动去启动他,不能实现开机自启动
android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
main.cpp \
interface_test_binder.cpp \
test_binder.cpp \
LOCAL_SHARED_LIBRARIES := liblog \
libm \
libc \
libutils \
libcutils \
libprocessgroup \
libbinder \
libc_malloc_debug \
libmemunreachable
LOCAL_MODULE := test_binder
LOCAL_INIT_RC := test_binder.rc
include $(BUILD_EXECUTABLE)
.rc文件
service test_binder /system/bin/test_binder
class core
group root readproc
writepid /dev/cpuset/system-background/tasks
写main函数,里面生成一个class的对象,并执行这个对象函数
#define LOG_TAG "performance"
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <sys/resource.h>
#include <utils/Log.h>
#include <utils/threads.h>
#include "test_binder.h"
using namespace android;
int main(int, char**) {
signal(SIGPIPE, SIG_IGN);
ProcessState::self()->setThreadPoolMaxThreadCount(6);
// start the thread pool
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
sp<test_binder> bindertest = new test_binder();
// initialize before clients can connect
sp<IServiceManager> sm(defaultServiceManager());
sm->addService(String16(test_binder::getServiceName()), bindertest, false);
while (1) {
sleep(100000);
}
return 0;
}
.h文件中声明一个类,这样就可以定义他了,这里使用了binder通信,所以需要重写onstact函数(这个之前好像看过)
#ifndef ANDROID_PERFORMANCE_H
#define ANDROID_PERFORMANCE_H
#include <utils/RefBase.h>
#include "interface_test_binder.h"
namespace android {
class test_binder : public Bntest_binder
{
public:
static char const* getServiceName() {
return "test_binder";
}
test_binder();
virtual ~test_binder();
int inited;
// 必须在这个类里面重写onTransact函数
virtual status_t onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags);
};
}
#endif
对应的.cpp的文件
#define LOG_TAG "test_binder"
#include <stdint.h>
#include <sys/types.h>
#include <errno.h>
#include <math.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <private/android_filesystem_config.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/MemoryHeapBase.h>
#include <binder/PermissionCache.h>
#include <utils/String8.h>
#include <utils/String16.h>
#include "test_binder.h"
namespace android {
test_binder::test_binder() : Bntest_binder()
{
ALOGE("test_binder --- oncreate..");
}
test_binder::~test_binder()
{
ALOGE("test_binder --- Destory..");
}
status_t test_binder::onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags){
status_t res = Bntest_binder::onTransact(code, data, reply, flags);
ALOGD(" enter test_binder::onTransact \n");
return res;
}
}
定义native层的binder通信接口
#ifndef IPERFORMANCE_H
#define IPERFORMANCE_H
#include <binder/IInterface.h>
#include <binder/Parcel.h>
namespace android {
class Itest_binder: public IInterface {
public:
DECLARE_META_INTERFACE(test_binder);
};
class Bntest_binder: public BnInterface<Itest_binder> {
public:
virtual status_t onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags = 0);
};
}; // namespace android
#endif
对应的.cpp文件
#define LOG_TAG "test_binder"
#include <binder/Parcel.h>
#include <utils/String8.h>
#include <utils/String16.h>
#include <cutils/jstring.h>
#include "interface_test_binder.h"
namespace android {
#define PACKAGE_NAME "com.android.server.test_binder"
class Bptest_binder: public BpInterface<Itest_binder> {
public:
explicit Bptest_binder(const sp<IBinder>& impl)
: BpInterface<Itest_binder>(impl) {
}
};
IMPLEMENT_META_INTERFACE(test_binder, PACKAGE_NAME);
status_t Bntest_binder::onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags)
{
ALOGD("enter Bntest_binder::onTransact\n");
return 0;
}
}; // namespace android
可以看到先是native的onTransact被调用,然后再到他的子类