android binder 通信

来源:互联网 发布:rest in peace知乎 编辑:程序博客网 时间:2024/05/30 22:48

今天写第一篇博客,写的不好请多多指教。

1.写 一个测试用的apk

 配置 Android.mk文件,如下

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := Hello
LOCAL_JNI_SHARED_LIBRARIES := libJniAdd //这是我们要创建的jni库

LOCAL_PROGUARD_ENABLED := disabled
include $(BUILD_PACKAGE)

2.创建jni库,新建文件夹添加入下文件

配置Android.mk文件,如下

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
    shy_luo_hello_Hello.cpp \
    onload.cpp

LOCAL_C_INCLUDES += \
    $(JNI_H_INCLUDE) \

LOCAL_SHARED_LIBRARIES := \
    libutils \
    libAdd \
    libnativehelper \
    liblog

LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:= libJniAdd
LOCAL_PRELINK_MODULE:=false

include $(BUILD_SHARED_LIBRARY)

onload.cpp 文件如下

#include "JNIHelp.h"
#include "jni.h"
#include "utils/Log.h"
#include "utils/misc.h"

namespace android {
int register_android_server_addservice(JNIEnv* env);

};

using namespace android;
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;


    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        ALOGE("GetEnv failed!");
        return result;
    }


    ALOG_ASSERT(env, "Could not retrieve the env!");
    register_android_server_addservice(env);
    return JNI_VERSION_1_4;
}

shy_luo_hello_Hello.cpp 如下

#include "jni.h"
#include "JNIHelp.h"
#include "../Add/Add.h"
#include <utils/Log.h>

namespace android {

static jint nativeStart(JNIEnv* env, jclass clazz) {
        Add* p = new Add();
        int r = p->setN(5);
        return r;
}

static JNINativeMethod gAddserviceMethods[] = {
    { "nativeStart", "()I",
            (void*) nativeStart },
};

int register_android_server_addservice(JNIEnv* env) {
    int res = jniRegisterNativeMethods(env, "shy/luo/hello/Hello",
            gAddserviceMethods, NELEM(gAddserviceMethods));
    LOG_FATAL_IF(res < 0, "Unable to register native methods.");

    return 0;
}

}

3.创建新文件夹添加libadd库

配置Android.mk文件,如下

LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=Add.cpp
LOCAL_SHARED_LIBRARIES:=libutils libbinder liblog
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=libAdd
LOCAL_PRELINK_MODULE:=false
include $(BUILD_SHARED_LIBRARY)

Add.cpp文件

#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "Add.h"
#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
namespace android
{
          sp<IBinder>  binder;
          int Add::setN(int n)
          {
             getAddService();
             Parcel data, reply;
             data.writeInt32(getpid());
             data.writeInt32(n);
             binder->transact(0, data, &reply);
             int r = reply.readInt32();
             return r;
           }
          
           void Add::getAddService()
           {
               sp<IServiceManager>  sm = defaultServiceManager();
                    binder = sm->getService(String16("test"));
               if(binder == 0){
                              return;
                              }
           }
}

Add.h文件

#ifndef _Included_Add
#define _Included_Add
namespace android
{
      class Add
      {
        public:
               int setN(int n);
        private:
               static void  getAddService();
      };
}
#endif


在如下文件中加入系统库

build/target/product/base.mk

   libAdd \

 libJniAdd \


4.新建文件夹添加service

在如下文件中加入系统库

build/target/product/base.mk

addservice \

配置Android.mk文件,如下

LOCAL_PATH:=$(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:=AddService.cpp  addservice.cpp
LOCAL_SHARED_LIBRARIES:=libutils libbinder liblog
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=addservice
LOCAL_PRELINK_MODULE:=false
include $(BUILD_EXECUTABLE)


添加AddService.cpp文件

#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "AddService.h"

namespace android
{
    static struct sigaction oldact;
    static pthread_key_t sigbuskey;
    int AddService::Instance()
    {
        int ret = defaultServiceManager()->addService(
                String16("test"), new AddService());
        return ret;
    }

    AddService::AddService()
    {
        pthread_key_create(&sigbuskey,NULL);
    }

    AddService::~AddService()
    {
        pthread_key_delete(sigbuskey);
    }

    status_t AddService::onTransact(uint32_t code, 
                                 const Parcel& data, 
                                 Parcel* reply,
                                 uint32_t flags)
    {
        switch(code)
        {
        case 0: 
            {
                pid_t pid = data.readInt32();
                int num = data.readInt32();
                num += 1000;
                reply->writeInt32(num);
                return NO_ERROR;
            } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
        }
    }
}

添加addservice.cpp文件

#include <stdio.h>
#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 "AddService.h"


using namespace android;


int main(int arg, char** argv)
{
     sp<ProcessState>  proc(ProcessState::self());
     sp<IServiceManager> sm = defaultServiceManager();
     int ret = AddService::Instance();
     ProcessState::self()->startThreadPool();
     IPCThreadState::self()->joinThreadPool();
     return 0;
}

添加系统service

/device/rockchip/rksdk/init.rc

service  addserviceh  /system/bin/addservice
    class main
    user root



原创粉丝点击