Android中實現native服務利用binder與應用通信

主要講解一下,Android的上層應用通過binder機制調用native服務,下面這個圖,主要用來描述,我們在實現binder相關的bp和bn端的時候,基本的類圖。



上圖基本說明了一下 如何利用binder實現底層的服務,基本實現就是上面這樣一個模板,下面我會就我自己實現的一個demo,詳細說明一下,如何來寫code,之前也是看別人的博客,參考到的例子,但是例子在我這有很多問題,就改了改,反正是可以正常使用了。



上圖是基本說明了,我的這個demo的類繼承關係,這個demo主要實現了Java上層利用binder調用C++層的bn端,而後C++的bn端再將請求返回給上層的,這樣一個實例,這個實例主要是做了一個加法運算。


C++實現的native服務


AddService.h

#ifndef ANDROID_GUILH_ADD_SERVICE_H

#define ANDROID_GUILH_ADD_SERVICE_H


#include <utils/threads.h>
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <binder/IBinder.h>
#include <binder/Binder.h>

#include <binder/IBinder.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/misc.h>
#include <binder/Parcel.h>
#include <utils/StringArray.h>
#include <utils/threads.h>
#include <cutils/properties.h>



#include "jni.h"
#include "JNIHelp.h"


#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <assert.h>

namespace android

{

class AddService : public BBinder

{

	mutable Mutex mLock;

	int32_t mNextConnId;

public:

	static int instantiate();

	AddService();

	virtual ~AddService();

	virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);

};

}

#endif


AddService.cpp

#include <AddService.h>

#include <binder/IServiceManager.h>
#include <utils/Debug.h>
#include <utils/Log.h>
#include <binder/IPCThreadState.h>
#include <binder/Parcel.h>
//#include <binder/IBinder.h>
#include <utils/String8.h>

#include <binder/IBinder.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/misc.h>
#include <binder/Parcel.h>
#include <utils/StringArray.h>
#include <utils/threads.h>
#include <cutils/properties.h>

#include "jni.h"
#include "JNIHelp.h"
//#include "android_util_Binder.h"

#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <assert.h>

namespace android {

static struct sigaction oldact;

static pthread_key_t sigbuskey;
sp<IBinder> binder;
int AddService::instantiate() {

	LOGE("AddService instantiate");

	int r = defaultServiceManager()->addService(

	String16("flyfot.add"), new AddService());

	LOGE("AddService r = %d/n", r);

	return r;

}

AddService::AddService()

{
	LOGV("AddService created");

	mNextConnId = 1;

	pthread_key_create(&sigbuskey, NULL);

}

AddService::~AddService()

{
	pthread_key_delete(sigbuskey);

	LOGV("AddService destroyed");

}

status_t AddService::onTransact(

uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
	LOGE("AddDemo", "AddService.cpp:onTransact:" + code);
	switch (code) {

	case 0: {


		LOGI("AddDemo", "addservice::onTransact");
		//pid_t pid = data.readInt32();
		//int a = data.readInt32();
	//	int b = data.readInt32();

		//int num = a + b;


		//sp < IServiceManager > sm = defaultServiceManager();

		LOGE("AddService","start get java service");

		//binder = sm->getService(
		//		String16("zx.java"));
		//String16 info = data.re
		//data.en
		binder = data.readStrongBinder();
		LOGE("AddService.cpp","data.readStrongBinbder");
		if (binder == 0) {
			LOGE("AddService", "get java local service failed");
			return NO_ERROR;
		}
		int a = data.readInt32();
		int b = data.readInt32();
		Parcel _data_sec ;
		//String16 interface = "cn.zx.callback";
		_data_sec.writeInterfaceToken(String16("cn.zx.secrity.RemoteSecrityCallback"));
		_data_sec.writeInt32(a);
		_data_sec.writeInt32(b);
		LOGE("AddService", "get java local service sucessful");
		binder->transact(0,_data_sec, reply);

		return NO_ERROR;

	}
		break;
	default:

		return BBinder::onTransact(code, data, reply, flags);
	}
}

}
;


。。。。。待續/。。。。。

發佈了66 篇原創文章 · 獲贊 30 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章